試圖做的這相當於在PHP - 和失敗:):在頭髮送身份驗證PHP捲曲
curl -H "X-abc-AUTH: 123456789" http://APIserviceProvider=http://www.cnn.com;
「123456789」是API密鑰。命令行語句正常工作。
PHP代碼(不工作):
$urlToGet = "http://www.cnn.com";
$service_url = "http://APIserviceProvider=$urlToGet";
//header
$contentType = 'text/xml'; //probably not needed
$method = 'POST'; //probably not needed
$auth = 'X-abc-AUTH: 123456789'; //API Key
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
//does not work
// curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-type: ' .
// $contentType . '; auth=' . $auth));
//works! (THANKS @Fratyr for the clue):
curl_setopt($ch, CURLOPT_HTTPHEADER, Array($auth));
//this works too (THANKS @sergiocruz):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Some_custom_header: 0',
'Another_custom_header: 143444,12'
));
//exec
$data = curl_exec($ch);
echo $data;
curl_close($ch);
任何想法?
爲什麼你使用'(服務URL更改爲正確的得到這個工作) Content-Type「如果你的命令行例子有一個'X-abc-AUTH:'頭文件? – mario
我收到「需要內容類型」錯誤。但我只是想出來了!我已經更新了上面的代碼。 – ven