Reputation: 105
I have this code that represents a curl code:
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"
}'
I need to represent this same CURL code using PHP, for this I try the following code:
$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 is supposed to be the header, but what are -u and -d and how can I send them ?
Upvotes: 0
Views: 68
Reputation: 19154
-u
is for Authorization header
$encodedAuth = base64_encode("consumer_key:consumer_secret");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization : Basic ".$encodedAuth));
or using this
curl_setopt($ch, CURLOPT_USERPWD, "consumer_key:consumer_secret");
-d
is for data or request body
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"regular_price":"24.54"}');
you also need to set custom request method for PUT
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
Upvotes: 2