2017-08-24 90 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