2012-11-02 54 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 ;