使用curl在标头中传递参数

时间:2016-01-04 14:52:11

标签: php web-services curl

我有像

这样的参数
$A='ZAXGHGN';
$INPUT='<?xml version="1.0" encoding="utf-8"?><a><b>test data</b></a>';
$array= array('A:'.$A,'INPUT:'.$INPUT);

以下是我的卷曲代码

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$array );

    $data = curl_exec($ch); 
    if(curl_errno($ch))
    print curl_error($ch);
    else
    curl_close($ch);
     echo "<pre>"; print_r($data);exit;

当我尝试执行代码时,我遇到了错误。

输出

错误请求 - 标头无效 HTTP错误400.请求的标头名称无效。

1 个答案:

答案 0 :(得分:1)

尝试这样的事情

$A = 'ZAXGHGN';
$INPUT = '<?xml version="1.0" encoding="utf-8"?><a><b>test data</b></a>'; // is this a valid XML???

$headers = array(
    "Content-type: text/xml",
    "Content-length: " . strlen($INPUT),
    "A: " . $A,
    // like this  if you want to have this value  in the header ... but to put the xml inside the header info...
    "Connection: close"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $INPUT); // send xml data using POST 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
if(curl_errno($ch))
    print curl_error($ch);
else
    curl_close($ch);
echo "<pre>";
print_r($data);
exit;