标头参数发布请求php(curl)

时间:2012-08-09 12:00:40

标签: php http curl

我正在尝试构建一个发布其他web服务的php脚本我正在使用culr,你可以看到,问题是在API webserver docs中请我发送一些头参数,字面意思 “对于每个http调用,您需要添加以下标头参数: APIuserID, API密码和 的responseType“

所以我的问题是如何在标头请求中添加自定义参数?感谢

<?php 
function curl_post($url, array $post = NULL, array $options = array()) 
{ 
$defaults = array( 
    CURLOPT_POST => 1, 
    CURLOPT_HEADER => 0, 
    CURLOPT_URL => $url, 
    CURLOPT_FRESH_CONNECT => 1, 
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_FORBID_REUSE => 1, 
    CURLOPT_TIMEOUT => 4, 
    CURLOPT_SSL_VERIFYHOST=>0,
    CURLOPT_POSTFIELDS => http_build_query($post) 
); 

$ch = curl_init(); 
curl_setopt_array($ch, ($options + $defaults)); 
if( ! $result = curl_exec($ch)) 
{ 
    trigger_error(curl_error($ch)); 
} 
curl_close($ch); 
return $result; 
} 

$result=curl_post('https://www.serverPath/serverAPI',array('a'=>'a','b'=>'b'));

echo $result;

&GT;

1 个答案:

答案 0 :(得分:7)

您是否尝试过将这些参数添加到httpheader?

curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array(
    "Content-Type: text/xml",
    "APIuserID: $id", 
    "APIpassword: $password", 
    "Content-length: ".strlen($data)
)); 

此外,您可以使用:http://php.net/manual/en/soapheader.soapheader.php