2017-08-24 87 views
0

我試圖用API連接API。但我得到的是「命令參數是必需的」。爲了工作,我需要發送RAW Body數據,它需要使用PUT方法。帶有JSON和原體的PHP cURL

JSON數據需要像

{ 
"OutputID":"Some key", 
"Activate":true, 
} 

這裏是我的代碼

$curl = curl_init($URL); 

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

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 

$jsonData = array(
    'OutputID' => 'Some key', 
    'Activate' => true, 
); 

$jsonDataEncoded = json_encode($jsonData); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonDataEncoded); 

curl_setopt($curl, CURLOPT_HTTPHEADER, array( 
    'Content-Type: application/json', 
    'Content-Length: 0', 
    $Authorization_Token_Key)); 


if(curl_exec($curl) === false) 
{ 
    echo 'Curl error: ' . curl_error($curl); 
} 
else 
{ 
    echo ''; 
} 

如果我和郵差嘗試一下它的工作原理,但不是在PHP腳本。

+0

你能在你的意思是什麼拓展它不工作在PHP? – Mike

+0

既然你想使用'PUT'方法,我想你應該刪除'curl_setopt($ curl,CURLOPT_POST,true);' –

+0

你的'Content-Length'不是0. – ishegg

回答

0

@Patrick

你在哪裏吧:)

我刪除

curl_setopt($curl, CURLOPT_POST, true); 

它就像一個魅力

感謝

+0

而是嘗試使用'curl_setopt($ curl ,CURLOPT_PUT,true);'。請參閱https://stackoverflow.com/questions/19257295/send-put-request-with-php-curl – Mike