如何使用 PHP cURL 发布 JSON 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11079135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to POST JSON Data With PHP cURL?
提问by user1463076
Here is my code,
这是我的代码,
$url = 'url_to_post';
$data = array(
"first_name" => "First name",
"last_name" => "last name",
"email"=>"[email protected]",
"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
所以,即使在我自己的服务器上我也没有得到正确的数据,它是空数组。我想使用 json 实现 REST,如http://docs.shopify.com/api/customer#create
回答by Stephen M. Harris
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:
您发布的 json 不正确——但即使它是正确的,您也无法使用print_r($_POST)(阅读原因)进行测试。相反,在您的第二页上,您可以使用 获取传入请求file_get_contents("php://input"),其中将包含 POSTed json。要以更易读的格式查看接收到的数据,请尝试以下操作:
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:
在您的代码中,您指示的是Content-Type:application/json,但您并未对所有 POST 数据进行 json 编码——仅对“客户”POST 字段的值进行了编码。相反,请执行以下操作:
$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 libraryinstead of interfacing with the Shopify API directly yourself.
旁注:您可能会受益于使用第三方库而不是自己直接与 Shopify API 接口。
回答by stellar roki
$url = 'url_to_post';
$data = array("first_name" => "First name","last_name" => "last name","email"=>"[email protected]","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) );
$postdata = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
This code worked for me. You can try...
这段代码对我有用。你可以试试...
回答by UltraInstinct
Replace
代替
curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string));
with:
和:
$data_string = json_encode(array("customer"=>$data));
//Send blindly the json-encoded string.
//The server, IMO, expects the body of the HTTP request to be in JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
I dont get what you meant by "other page", I hope it is the page at: 'url_to_post'. If that page is written in PHP, the JSON you just posted above will be read in the below way:
我不明白您所说的“其他页面”是什么意思,我希望它是位于:'url_to_post' 的页面。如果该页面是用 PHP 编写的,那么您刚刚在上面发布的 JSON 将按以下方式读取:
$jsonStr = file_get_contents("php://input"); //read the HTTP body.
$json = json_decode($jsonStr);
回答by Damini Mahawer
Please try this code:-
请试试这个代码:-
$url = 'url_to_post';
$data = array("first_name" => "First name","last_name" => "last name","email"=>"[email protected]","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(array("customer" =>$data));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo "$result";
回答by Prasad Rajapaksha
Try this example.
试试这个例子。
<?php
$url = 'http://localhost/test/page2.php';
$data = array("first_name" => "First name","last_name" => "last name","email"=>"[email protected]","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) );
$ch=curl_init($url);
$data_string = urlencode(json_encode($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Your page2.php code
你的 page2.php 代码
<?php
$datastring = $_POST['customer'];
$data = json_decode( urldecode( $datastring));
?>
回答by Okonomiyaki3000
Try like this:
像这样尝试:
$url = 'url_to_post';
// this is only part of the data you need to sen
$customer_data = array("first_name" => "First name","last_name" => "last name","email"=>"[email protected]","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) );
// As per your API, the customer data should be structured this way
$data = array("customer" => $customer_data);
// And then encoded as a json string
$data_string = json_encode($data);
$ch=curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array('Content-Type:application/json', 'Content-Length: ' . strlen($data_string)))
));
$result = curl_exec($ch);
curl_close($ch);
The key thing you've forgotten was to json_encode your data. But you also may find it convenient to use curl_setopt_array to set all curl options at once by passing an array.
您忘记的关键是对您的数据进行 json_encode。但是您也可能会发现使用 curl_setopt_array 通过传递一个数组来一次设置所有 curl 选项很方便。

