2012-11-02 52 views
5

試圖做的這相當於在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); 

任何想法?

+1

爲什麼你使用'(服務URL更改爲正確的得到這個工作) Content-Type「如果你的命令行例子有一個'X-abc-AUTH:'頭文件? – mario

+0

我收到「需要內容類型」錯誤。但我只是想出來了!我已經更新了上面的代碼。 – ven

回答

13

爲了讓自定義頁眉到您的捲髮,你應該做的事情如下所示:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Some_custom_header: 0', 
    'Another_custom_header: 143444,12' 
)); 

因此,以下應該在你的情況下工作(給定的X-ABC-AUTH是唯一的頭您需要發送以上):

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-abc-AUTH: 123456789' // you can replace this with your $auth variable 
)); 

如果您需要更多的自定義標題,所有你需要做的就是在增加了curl_setopt內的陣列。

我希望這有助於:)

+1

感謝隊友這工作很好! – ven

+0

沒有問題的所有:) – sergiocruz

0
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'auth=' . $auth 
)); 
+0

這甚至不是有效的http。 – Evert

+0

那麼爲什麼上面的同一個帖子得到+,我有一個 - ? :) – deb0rian

+0

這幾乎工作!這使它工作:curl_setopt($ ch,CURLOPT_HTTPHEADER,Array($ auth)); – ven

0

您只設置一個請求標題,而不是你想要的兩個。你可以做到這一點的例子是這樣的:

// input 
$urlToGet = "http://www.cnn.com"; 

// url 
$service_url = sprintf("http://APIserviceProvider=%s", urlencode($urlToGet)); 

//header 
$contentType = 'Content-type: text/xml'; //probably not needed 
$auth  = 'X-abc-AUTH: 123456789'; //API Key 
$method  = 'POST'; //probably not needed 

// curl init 
$ch = curl_init($service_url); 
curl_setopt_array($ch, [ 
    CURLOPT_RETURNTRANSFER => true, 
    CURLINFO_HEADER_OUT => true, 
    CURLOPT_HTTPHEADER  => [ 
     $contentType, 
     $auth, 
    ], 
]); 

// curl exec 
$data = curl_exec($ch); 
curl_close($ch); 

// output 
echo $data; 

+0

感謝您的建議 - 似乎它會工作,但它沒有:(我也改變了服務的網址+鍵。再次感謝 – ven

+0

''curl_exec'''''' var_dump(curl_error($ ch));'' – hakre

3

使用以下語法

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$headers = array(); 
$headers[] = 'X-abc-AUTH: 123456789'; 
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; 
$headers[] = 'Accept-Encoding: gzip, deflate'; 
$headers[] = 'Accept-Language: en-US,en;q=0.5'; 
$headers[] = 'Cache-Control: no-cache'; 
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8'; 
$headers[] = 'Host: 202.71.152.126'; 
$headers[] = 'Referer: http://www.example.com/index.php'; //Your referrer address 
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0'; 
$headers[] = 'X-MicrosoftAjax: Delta=true'; 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

$server_output = curl_exec ($ch); 

curl_close ($ch); 

print $server_output ;