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

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

问题描述

这是我的代码,

$url = 'url_to_post';$数据=数组(名字"=>名",姓氏" =>姓","email"=>"email@gmail.com",地址" =>大批 (地址 1" =>一些地址",城市" =>城市",国家" =>"CA",名字"=>母亲",姓氏" =>"姓氏",电话" =>555-1212",省" =>在",邮编"=>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,大批('内容类型:应用程序/json','内容长度:'.strlen($data_string)));$result = curl_exec($ch);curl_close($ch);

在其他页面,我正在检索帖子数据.

 print_r ($_POST);

输出是

HTTP/1.1 200 OK日期:2012 年 6 月 18 日星期一 07:58:11 GMT服务器:阿帕奇X-Powered-By:PHP/5.3.6变化:接受编码连接:关闭内容类型:文本/html大批 ( )

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

解决方案

您 POST 的 json 不正确 -- 但即使它是正确的,您也无法使用 print_r($_POST)(在此处阅读原因).相反,在您的第二个页面上,您可以使用 file_get_contents("php://input") 获取传入请求,其中将包含 POSTed json.要以更易读的格式查看接收到的数据,请尝试以下操作:

echo '

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

在您的代码中,您指明了 Content-Type:application/json,但您并未对所有 POST 数据进行 json 编码——只有客户"POST 字段的值.相反,请执行以下操作:

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

旁注:您可能会受益于使用第三方库直接与 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天全站免登陆