简体   繁体   中英

Pass parameters in header using curl

I have params like

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

Below is my curl code

$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;

When i try to execute the code i am getting below error.

Output :

Bad Request - Invalid Header HTTP Error 400. The request has an invalid header name.

try something like this

$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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM