9

试图在 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);

有任何想法吗?

4

4 回答 4

21

为了将自定义标头放入您的 curl 中,您应该执行以下操作:

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 中的数组中。

我希望这有帮助 :)

于 2012-11-02T00:24:54.710 回答
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 ;
于 2015-02-05T15:38:37.670 回答
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;

(将服务网址更改为正确的网址以使其正常工作)

于 2012-11-02T00:29:56.837 回答
0
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'auth=' . $auth
));
于 2012-11-02T00:19:45.110 回答