简体   繁体   中英

Posting JSON with PHP CURL

I'm tasked with Posting to an API that only accepts JSON. I am using PHP's CURL to accomplish this.

I have posted to API's before with no issue, but never with JSON, something I am not familiar with, I have tried to research this on my own and solve the problem, but I'm not having any luck.

When talking with someone at the company I am trying to post to, I was told that my server is hitting their server, the only thing wrong is the body of my post is not properly formatted JSON.. (no one at this company knows php :( so no help there)

Here is my code:

$jarr = array("ProviderID" => "L005A", "FirstName" => $first, 
           "LastName" => $last, "PhoneNumber" => $PhoneNumber, 
           "PhoneNumberType" => $PhoneNumberType);

$content = json_encode($jarr);

$curl_handle = curl_init("URL Im posting to");
 curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array(
'Accept: application/json;charset=utf-8',
    'Content-Type: application/json;charset=utf-8',
    'Expect: 100-continue',
    'Connection: Keep-Alive'));
    curl_setopt($curl_handle, CURLOPT_HEADER, 1);
    curl_setopt($curl_handle, CURLOPT_POST, 0);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $content);
    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    $first = curl_exec($curl_handle);
    curl_close($curl_handle);
    echo "Result ";
    echo $first;

From the documentation I was given, the only parameters I must follow are:
Method: Post

Headers:
Accept: application/json;charset=utf-8
Content-Type: application/json;charset=utf-8
Expect: 100-continue
Connection: Keep-Alive

Currently, when I execute the script, I get a return of:

Result HTTP/1.1 100 Continue HTTP/1.1 400 Bad Request Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Thu, 01 Nov 2012 15:43:18 GMT Content-Length: 26 {"record.ProviderID":[""]}

So the 400 error is caused because my body is not properly formatted. My question, how would I format the data I need to send in proper JSON format?

From what I have read, this code should do it, but it does not:

$jarr = array("ProviderID" => "L005A", "FirstName" => $first,
           "LastName" => $last, "PhoneNumber" => $PhoneNumber,
           "PhoneNumberType" => $PhoneNumberType);

$content = json_encode($jarr);

I faced the same problem. After debugging, found out that while sending JSON string, not quoting or double quoting the Integer values were the culprit. You need to send double quoted integer values as a string.

Eg:

{"PhoneNumber":9876543210}

or

{"PhoneNumber":"9876543210"}

will not work.

but

{"PhoneNumber":"N_9876543210"}

will work.

Will update as soon as I figure it out what exactly is the root cause of this problem. If someone knows, please update soon.

Edit: The problem solved by URL Encoding. I hope it solves in your case also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM