繁体   English   中英

代表PHP中的CURL代码吗?

[英]Represent CURL code in PHP?

我有代表卷曲代码的这段代码:

curl -X PUT https://example.com/wp-json/wc/v2/products/794 \
    -u consumer_key:consumer_secret \
    -H "Content-Type: application/json" \
    -d '{
  "regular_price": "24.54"
}'

我需要使用PHP表示相同的CURL代码,为此,我尝试以下代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/wp-json/wc/v2/products/794');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-Type: application/json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

-H应该是标题,但是-u-d是什么,如何发送它们?

-u用于授权标头

$encodedAuth = base64_encode("consumer_key:consumer_secret");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization : Basic ".$encodedAuth));

或使用这个

curl_setopt($ch, CURLOPT_USERPWD, "consumer_key:consumer_secret");

-d用于数据或请求正文

curl_setopt($ch, CURLOPT_POSTFIELDS, '{"regular_price":"24.54"}');

您还需要为PUT设置自定义请求方法

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM