如何使用PHP cURL发布JSON数据? [英] How to POST JSON Data With PHP cURL?

查看:147
本文介绍了如何使用PHP cURL发布JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,

  $ url ='url_to_post'; 
$ data = array(first_name=>名字,last_name=>姓氏,email=>email@gmail.com,地址=> ; 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 OK 
Date:Mon,18 Jun 2012 07:58:11 GMT
服务器:Apache
X-Powered-By:PHP / 5.3.6
Vary:Accept-Encoding
Connection:close
Content-Type:text / html

Array()
pre>

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

解决方案

您错误地提交了json - 但即使它是正确的,使用 print_r($ _ POST)阅读原因) 。您可以在第二页上使用 file_get_contents(php:// input)来命名传入的请求,它将包含POSTed json 。要以更可读的格式查看接收的数据,请尝试以下操作:

  echo'< pre>'。print_r(json_decode file_get_contents(php:// input)),1)。'< / pre>'; 

在您的代码中,您指示 Content-Type:application / json ,但是你不是json编码所有的POST数据 - 只有customerPOST字段的值。而是这样做:

  $ ch = curl_init($ url); 
#设置请求通过POST发送json。
$ payload = json_encode(array(customer=> $ data));
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ payload);
curl_setopt($ ch,CURLOPT_HTTPHEADER,array('Content-Type:application / json'));
#返回响应而不是打印。
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
#发送请求。
$ result = curl_exec($ ch);
curl_close($ ch);
#打印响应。
echo< pre> $ result< / pre>;

Sidenote:您可能会受益于使用第三方库,而不是直接与Shopify API直接交流。


Here is my code,

    $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);

And at other page, I am retrieving post data.

    print_r ($_POST);

Output is

HTTP/1.1 200 OK
Date: Mon, 18 Jun 2012 07:58:11 GMT
Server: Apache
X-Powered-By: PHP/5.3.6
Vary: Accept-Encoding
Connection: close
Content-Type: text/html

Array ( ) 

So, I am not getting proper data even at my own server, it's empty array. I want to implement REST using json as at http://docs.shopify.com/api/customer#create

解决方案

You are POSTing the json incorrectly -- but even if it were correct, you would not be able to test using print_r($_POST) (read why here). Instead, on your second page, you can nab the incoming request using file_get_contents("php://input"), which will contain the POSTed json. To view the received data in a more readable format, try this:

echo '<pre>'.print_r(json_decode(file_get_contents("php://input")),1).'</pre>';

In your code, you are indicating Content-Type:application/json, but you are not json-encoding all of the POST data -- only the value of the "customer" POST field. Instead, do something like this:

$ch = curl_init( $url );
# Setup request to send json via POST.
$payload = json_encode( array( "customer"=> $data ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
echo "<pre>$result</pre>";

Sidenote: You might benefit from using a third-party library instead of interfacing with the Shopify API directly yourself.

这篇关于如何使用PHP cURL发布JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆