这是我的代码,
$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);
在另一页,我正在检索post数据。
print_r ($_POST);
输出是
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 ( )
因此,即使在我自己的服务器上,我也得不到正确的数据,它是空数组。我想在http://docs.shopify.com/api/customer#create使用json实现REST
你不正确地发布了json——但即使它是正确的,你也不能使用print_r($_POST)进行测试(阅读这里的原因)。相反,在第二个页面上,您可以使用file_get_contents("php://input")获取传入请求,其中将包含post json。要以更可读的格式查看接收到的数据,请尝试以下方法:
echo '<pre>'.print_r(json_decode(file_get_contents("php://input")),1).'</pre>';
在您的代码中,您指示Content-Type:application/json,但您没有对所有POST数据进行json编码——只对“customer”POST字段的值进行json编码。相反,你可以这样做:
$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>";
旁注:您可能会受益于使用第三方库,而不是直接自己与Shopify API接口。
取代
curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string));
:
$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);
我不明白你说的“其他页面”是什么意思,我希望它是在:'url_to_post'的页面。如果该页面是用PHP编写的,那么你刚刚发布的JSON将以以下方式读取:
$jsonStr = file_get_contents("php://input"); //read the HTTP body.
$json = json_decode($jsonStr);
你不正确地发布了json——但即使它是正确的,你也不能使用print_r($_POST)进行测试(阅读这里的原因)。相反,在第二个页面上,您可以使用file_get_contents("php://input")获取传入请求,其中将包含post json。要以更可读的格式查看接收到的数据,请尝试以下方法:
echo '<pre>'.print_r(json_decode(file_get_contents("php://input")),1).'</pre>';
在您的代码中,您指示Content-Type:application/json,但您没有对所有POST数据进行json编码——只对“customer”POST字段的值进行json编码。相反,你可以这样做:
$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>";
旁注:您可能会受益于使用第三方库,而不是直接自己与Shopify API接口。
请试试这个代码:-
$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(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";
试着这样做:
$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@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" ) );
// 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);
您忘记的关键是json_encode您的数据。但是您也可能会发现,通过传递一个数组,使用curl_setopt_array一次性设置所有curl选项是很方便的。
试试这个例子。
<?php
$url = 'http://localhost/test/page2.php';
$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" ) );
$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;
?>
你的page2.php代码
<?php
$datastring = $_POST['customer'];
$data = json_decode( urldecode( $datastring));
?>