T W
T W

Reputation: 459

Curl request - invalid xml request when posting user&password after xml

I need to send the following request, which is XML with a username and password on the end of the xml. I can get this to work as a URL when I paste into the browser:

URL?XML=<>...</>&user=123456&password=123456

But when I try to create the curl call it says it's an invalid XML request.

I've tried having the user&password within the POSTFIELDS. I've also tried setting them as a variable before $trial ='&user=123456&password=123456' but still can't seem to get it to work.

$xml = <<<ENDOFXML
<?xml version="1.0" encoding="UTF-8"?><xmldata>...</xmldata>
ENDOFXML;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode($xml).urlencode('&user=123456&password=123456'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_close($ch);

Upvotes: 1

Views: 627

Answers (1)

Marek
Marek

Reputation: 7423

This is wrong:

curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode($xml).urlencode('&user=123456&password=123456'));

urlencode only the variable names and the values (or only values if you know the variable names are ok):

curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode($xml).'&user=' . urlencode('123456').'&password=' . urlencode('123456'));

Upvotes: 1

Related Questions