如何使用PHP curl發(fā)布JSON數(shù)據(jù)?這是我的密碼 $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);在另一頁,我正在檢索帖子數(shù)據(jù)。 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 ( )所以,即使在我自己的服務(wù)器上,我也沒有得到正確的數(shù)據(jù),它是空數(shù)組。我希望使用json實(shí)現(xiàn)REST,如http://docs.shopify.com/api/customer#create
如何使用PHP curl發(fā)布JSON數(shù)據(jù)?
ibeautiful
2019-07-17 16:08:02