简体   繁体   English

使用PHP CURL发布JSON

[英]Posting JSON with PHP CURL

I'm tasked with Posting to an API that only accepts JSON. 我的任务是发布到仅接受JSON的API。 I am using PHP's CURL to accomplish this. 我正在使用PHP的CURL完成此操作。

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. 我以前没有发布过API的问题,但是从来没有发布过JSON,这是我不熟悉的东西,我试图自己研究并解决问题,但是我没有任何运气。

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) 与我要发布的公司的某人交谈时,有人告诉我我的服务器正在碰到他们的服务器,唯一的错误是我的发布正文未正确格式化JSON ..(该公司的没人知道php :(所以没有帮助)

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 接受:application / json; charset = utf-8
Content-Type: application/json;charset=utf-8 内容类型:application / json; charset = utf-8
Expect: 100-continue 期望:100-继续
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; 结果HTTP / 1.1 100继续HTTP / 1.1 400错误的请求缓存控制:no-cache语法:no-cache内容类型: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":[""]} charset = utf-8过期:-1服务器:Microsoft-IIS / 7.5 X-AspNet版本:4.0.30319 X-Powered-by:ASP.NET日期:2012年11月1日星期四15:43:18 GMT内容长度:26 {“ record.ProviderID”:[“”]}

So the 400 error is caused because my body is not properly formatted. 因此出现400错误是因为我的身体格式不正确。 My question, how would I format the data I need to send in proper JSON format? 我的问题是,如何格式化需要以正确的JSON格式发送的数据?

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. 调试后,发现在发送JSON字符串时,未引号或双引号是造成Integer值的罪魁祸首。 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. 编辑:URL编码解决的问题。 I hope it solves in your case also. 我希望它也能解决您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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