2017-07-22 3 views
0

Ich habe diesen Code, der einen Curl-Code darstellt:CURL-Code in PHP darstellen?

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" 
}' 

Ich brauche diese gleiche ROTATION Code darstellen PHP, für diese versuche ich den folgenden 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 ist soll der Header sein, aber was sind -u und -d und wie kann ich sie senden?

Antwort

2

-u ist für Authorization-Header

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

oder diese mit -d

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

ist für Daten oder Anfrage Körper

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

Sie müssen auch individuelle Anforderungsverfahren für PUT gesetzt

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