猿问

请问如何使用PHP curl发布JSON数据?

如何使用PHP curl发布JSON数据?

这是我的密码

    $url = 'url_to_post';
    $data = array("first_name" => "First name","last_name" => "last name","email"=>"email@gmail.com","addresses" 
    => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" =>  "Mother","last_name"
     =>  "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) );
    $data_string = json_encode($data);
    $ch=curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string));
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER,
               array('Content-Type:application/json',
                      'Content-Length: ' . strlen($data_string))
               );

    $result = curl_exec($ch);
    curl_close($ch);

在另一页,我正在检索帖子数据。

    print_r ($_POST);

输出

HTTP/1.1 200 OKDate: Mon, 18 Jun 2012 07:58:11 GMTServer: ApacheX-Powered-By: PHP/5.3.6Vary: Accept-EncodingConnection: 
closeContent-Type: text/htmlArray ( )

所以,即使在我自己的服务器上,我也没有得到正确的数据,它是空数组。我希望使用json实现REST,如http://docs.shopify.com/api/customer#create


犯罪嫌疑人X
浏览 350回答 3
3回答

炎炎设计

您正在错误地发布json,但是即使它是正确的,您也无法使用print_r($_POST)&nbsp;(在这里读为什么)。相反,在第二个页面上,您可以使用file_get_contents("php://input"),其中将包含已发布的json..若要以更易读的格式查看所接收的数据,请尝试如下:echo&nbsp;'<pre>'.print_r(json_decode(file_get_contents("php://input")),1).'</pre>';在您的代码中,您指示Content-Type:application/json,但是您不是json-编码所有的POST数据-只有“Customer”POST字段的值。相反,可以这样做:$ch&nbsp;=&nbsp;curl_init(&nbsp;$url&nbsp;);#&nbsp;Setup&nbsp;request&nbsp;to&nbsp;send&nbsp;json&nbsp;via&nbsp;POST.$payload&nbsp;=&nbsp;json_encode(&nbsp;array(&nbsp;"customer"=>&nbsp;$data&nbsp;)&nbsp;);curl_setopt(&nbsp;$ch,&nbsp;CURLOPT_POSTFIELDS,&nbsp;$payload&nbsp;);curl_setopt(&nbsp;$ch,&nbsp;CURLOPT_HTTPHEADER,&nbsp;array('Content-Type:application/json'));#&nbsp;Return&nbsp;response&nbsp;instead&nbsp;of&nbsp;printing.curl_setopt(&nbsp;$ch,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;true&nbsp;);#&nbsp;Send&nbsp;request.$result&nbsp;=&nbsp;curl_exec($ch);curl_close($ch);#&nbsp;Print&nbsp;response.echo&nbsp;"<pre>$result</pre>";Sideenote:使用第三方图书馆而不是直接与Shopify API接口。

千万里不及你

$url&nbsp;=&nbsp;'url_to_post';$data&nbsp;=&nbsp;array("first_name"&nbsp;=>&nbsp;"First&nbsp;name","last_name"&nbsp;=>&nbsp;"last&nbsp;name","email"=>"email@gmail.com","addresses"&nbsp;=>&nbsp;array&nbsp;("address1"&nbsp;=>&nbsp;"some&nbsp;address"&nbsp;,"city"&nbsp;=>&nbsp;"city","country"&nbsp;=>&nbsp;"CA",&nbsp;"first_name"&nbsp;=>&nbsp;&nbsp;"Mother","last_name"&nbsp;=>&nbsp;&nbsp;"Lastnameson","phone"&nbsp;=>&nbsp;"555-1212",&nbsp;"province"&nbsp;=>&nbsp;"ON",&nbsp;"zip"&nbsp;=>&nbsp;"123&nbsp;ABC"&nbsp;)&nbsp;);$postdata&nbsp;=&nbsp;json_encode($data);$ch&nbsp;=&nbsp;curl_init($url);curl_setopt($ch,&nbsp;CURLOPT_SSL_VERIFYHOST,&nbsp;0);curl_setopt($ch,&nbsp;CURLOPT_SSL_VERIFYPEER,&nbsp;0);curl_setopt($ch,&nbsp;CURLOPT_POST,&nbsp;1);curl_setopt($ch,&nbsp;CURLOPT_POSTFIELDS,&nbsp;$postdata);curl_setopt($ch,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;1);curl_setopt($ch,&nbsp;CURLOPT_FOLLOWLOCATION,&nbsp;1);curl_setopt($ch,&nbsp;CURLOPT_HTTPHEADER,&nbsp;array('Content-Type:&nbsp;application/json'));$result&nbsp;=&nbsp;curl_exec($ch);curl_close($ch);print_r&nbsp;($result);这个密码对我有用。你可以试试.。
随时随地看视频慕课网APP
我要回答