1

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.

2
  • Does the request work without curl_setopt($ch, CURLOPT_HTTPHEADER,$array );? Commented Jan 4, 2016 at 15:33
  • Your code is working here. What are your PHP and cURL versions? Commented Jan 4, 2016 at 16:16

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

3 Comments

I just saw that but xml $INPUT should be passed with parameter name 'INPUT'
same data when i pass $A, $INPUT as header params using restclient i am getting response. But when i hit the curl with params as mentioned above(by coding) i am not getting response.
You have 2 variables .. $A and $INPUT. $A can be sent as part of the header information but the content of the $INPUT variable I don't think it can be part of the header in this format(XML).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.