Working with a cURL API

I'm working with a really basic cURL API. I haven't really worked with cURL before, so I'm doing my best to feel through things.

For the sake of this example, let's say that there are two values I can pass to the API: email and location.

Also, I have to use basic HTTP Authentication where the password is blank and the username is my API key.

To help you understand what I'm going for, this is the example provided to me in the API documentation:

$ curl -X POST https://therequestpath -u $API_KEY: \  
--form email=myemailaddress \   
--form location='mylocation' \
--form content-type=application/json  

At this point I really don't quite understand what I'm doing, but this is the code I've come up with so far (it isn't throwing any PHP errors, but it isn't doing what I want, either):

$username = 'myapikey';
$password = '';
$host = 'https://therequestpath';
$data = array('email' => 'myemailaddress', 'location' => 'mylocation');

$process = curl_init($host);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);

I'm not sure if I'm failing at the HTTP Authentication, passing the email / location values, or both. Any help would be appreciated.


The first command-line example shows application/json as the content-type, so you should use the same in your PHP script:

curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

Also you should URL-encode each POST field, like this :

$data = array('email' => urlencode('myemailaddress'), 'location' => urlencode('mylocation'));

Need Your Help

Why the text of this TextView is not changed?

android onkeydown

Firstly I have to say that I have been like 2 weeks researching on this, but didn´t found the answer to this...

opening a new page and passing a parameter to it with Javascript

javascript xhtml dhtml html-table

I have a JS function that processes XML I GET from a server to dynamically create a table as below