标头参数后请求 php ( curl)


header parameters post request php ( curl)

我正在尝试构建一个 php 脚本来发布我正在使用 culr 的其他 Web 服务 如您所见,问题出在 API 网络服务器文档中要求我发送一些标头参数,字面意思是"对于每个http调用,您需要添加以下标头参数:API用户ID,API密码和响应类型"

所以我的问题是如何将自定义参数添加到我的标头请求中。

谢谢
<?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;

?>

您是否尝试过将这些参数添加到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