0

I am connecting with an API via curl and POSTing to it.

The API support simply says that I should form my request as so:

"If you send a POST request to the correct URL with a content-type of JSON and a body like what's below you should be off to a good start:

 {
   "contact": {
     "email": "justin@myapi.com",
     "last_name": "Johnston",
     "company": "MyApi",
     "first_name": "Justin"
   }
 }

However, I think this is for a regular PHP post maybe and not for CURL which is what I am using.

So, the curl code I have is:

 //Set Curl options
 curl_setopt ( $ch, CURLOPT_URL, $url );
 curl_setopt($ch, CURLOPT_POST, 1); 
 curl_setopt($ch, CURLOPT_POSTFIELDS, "first_name=" . $db->f('fname') . "&last_name=" . $db->f('lname') . "&email=" . $db->f('Email') . "&company=" . $db->f('studio_name') . "");

However, when I run the POST, the response I get back from the API says:

 {"error":"Invalid parameters. Should be a hash in the form { 'contact' : { } }."}

So, I am assuming that is because my POSTFIELDS are not correct, and I understand I need to lead with "Contact", but I am not sure how to format it and haven't been able to find an example. Any idears?

Thanks very much for any help you could give! I really do appreciate it!!

Craig

CRAIG
  • 977
  • 2
  • 11
  • 25
  • 1
    I think the problem is you're sending a string as the parameter, where it should be a JSON object. just use json_encode() to encode an array of data and send that via POST instead of the string you're forming. – SharkofMirkwood Jul 01 '14 at 02:42
  • see if [this](http://stackoverflow.com/a/6213693/696600) helps. – tradyblix Jul 01 '14 at 02:42

1 Answers1

2

To expand on the comment I posted...

I think the problem is you're sending a string as the parameter, where it should be a JSON object. just use json_encode() to encode an array of data and send that via POST instead of the string you're forming

You need to create a JSON string (usually by encoding an array of data):

$data = array(
    "contact" => array(
        "first_name" => $db->f('fname'),
        "last_name"  => $db->f('lname'),
        "email"      => $db->f('Email'),
        "company"    => $db->f('studio_name')
    )
);
$data = json_encode($data);

This will give you the JSON you need to post, so just change your line of code setting the parameters to this:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Edit: As requested in a comment, I'll attempt to explain a little more:

The POST request is sent to the server, and you can send any data you want with the request. In the example given in the question, this is valid JSON and they have specified that the body of the request should also be JSON (I'm assuming in the same format):

{
  "contact": {
    "email": "justin@myapi.com",
    "last_name": "Johnston",
    "company": "MyApi",
    "first_name": "Justin"
  }
}

In my solution above I've explained how to get a string containing valid JSON for an array of data; this string can be sent as the contents of the request. The problem with what you were trying is that the whole data would have literally been sent like this:

first_name=Justin&last_name=Johnston&email=justin@myapi.com&company=MyApi

It's understandable to be confused, as is this is how you would get the data returned from an HTML form, and is quite a common way of sending variables via POST. However in this case you need to use JSON, which is why that won't work.

SharkofMirkwood
  • 11,483
  • 2
  • 17
  • 25
  • 1
    excellent answer. may want to explain to the OP the difference between a post request and the api request data contained therein. he/she seems to have confused the two – r3wt Jul 01 '14 at 02:50
  • 1
    @r3wt I've expanded on my answer in an attempt to make it clearer, feel free to edit if I didn't do a great job! – SharkofMirkwood Jul 01 '14 at 03:06
  • Ok, this worked for me. I didn't have the JSON header type set, so once I added that in, all was good and set. Thanks so much for your help!!! – CRAIG Jul 01 '14 at 05:06
  • 1
    @CRAIG No worries, buddy. Hope you understand more about how cURL and HTTP requests work now as well. – SharkofMirkwood Jul 01 '14 at 05:09
  • I do! This was very helpful!! – CRAIG Jul 02 '14 at 20:00