10000RubyPools
10000RubyPools

Reputation: 1202

How to make a POST request with PHP given the following HTTP + JSON

I've been trying to make a POST request with PHP with something called the WorkWave API. This is the code they provide for making a POST request to set the app's callback URL:

POST /api/v1/callback HTTP/1.0
Accept: application/json
X-WorkWave-Key: YOUR API KEY
Host: wwrm.workwave.com
Content-Type: application/json

{
  "url": "https://my.server.com/new-callback",
  "signaturePassword": "g394g732vhsdfiv34",
  "test": true
}

I'm pretty new to processing POST and GET requests, so I don't really understand what's going on here. What exactly is the blob of keys & values above the JSON brackets? How do I translate what is given here to PHP and what are the most important things to understand when doing so?

Upvotes: 5

Views: 16913

Answers (1)

Jeff Puckett
Jeff Puckett

Reputation: 40881

The first blob are the headers that you need to send with your request. The JSON is the post body payload.

<?php

$url = 'https://wwrm.workwave.com/api/v1/callback';

$data = '
{
  "url": "https://my.server.com/new-callback",
  "signaturePassword": "g394g732vhsdfiv34",
  "test": true
}
';

$additional_headers = array(                                                                          
   'Accept: application/json',
   'X-WorkWave-Key: YOUR API KEY',
   'Host: wwrm.workwave.com',
   'Content-Type: application/json'
);

$ch = curl_init($url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, $additional_headers); 

$server_output = curl_exec ($ch);

echo  $server_output;

Upvotes: 5

Related Questions