PUT请求更新网站中的数据

时间:2016-08-12 06:46:20

标签: php curl

我想使用Curl PUT请求更新网站中的价格字段。但我无法弄清楚代码中的错误。如果您能找到错误,那将会很有帮助。以下是我的代码。

$api_request_url = 'http://example.com/variants/SOMETHING';


$method_name = 'PUT';

$header = array(
"Accept: application/json, text/plain",
"Content-Type: application/json;charset=utf-8",
"Connection: keep-alive",
"Accept-Language: en-US,en;q=0.5",
"Accept-Encoding: gzip, deflate",
"Content-Length: 82",
"Referer: http://seller.example.com/catalog",
"Cache-Control: max-age=0, private, must-revalidate"
);

$payload = array(
//"variant:  'YEL510044' , 6",//
'price' => '600',
//"original_price: 999"//
);
$data_string = json_encode($payload);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);



curl_setopt($ch, CURLOPT_HTTPHEADER, $header);


curl_setopt($ch, CURLOPT_URL, $api_request_url);


curl_setopt($ch, CURLOPT_HEADER, TRUE);


curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


$api_response = curl_exec($ch);


$api_response_info = curl_getinfo($ch);
echo "$api_response";

curl_close($ch);

2 个答案:

答案 0 :(得分:2)

您尝试更改代码:

$data_string = json_encode($payload);

$data_string = http_build_query($payload);

答案 1 :(得分:0)

你应该尝试:

curl_setopt($ ch,CURLOPT_PUT,true);

取代

curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,'PUT');