簡體   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