0

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  
{"id":"750ea3d7"}

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);
curl_exec($process);
curl_close($process);

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

3
  • In the PHP, shouldn't you send application/json content type instead of application/xml ? Commented Dec 9, 2013 at 20:53
  • 1
    Also notice you use content-type in the command-line, but Content-Type in PHP. That can matter. And I also don't understand the last two lines of your command-line example - haven't you omitted a \ at the end of the fourth line? Commented Dec 9, 2013 at 21:56
  • @peci1 - The API accepts both json & xml. Right before I posted here, I changed my code from json to xml to see if it made a difference. I just forgot to remove it before I copied the code over. Thanks for the catch, though. The content-type case was one of my issues! Thanks for the good eye. I'm not sure I would have caught that on that my own any time soon. The second issue was the single quotes in the $data array. Once I removed those and changed the content-type case, everything worked. Commented Dec 10, 2013 at 0:35

1 Answer 1

2

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'));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tips. I changed the content-type to json. Also, I'm now using urlencode for the values. I'm still not getting the expected results, but thanks for helping me clean things up.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.