我需要发送以下请求,即xml末尾带有用户名和密码的XML。当我粘贴到浏览器中时,我可以将其作为URL工作:
URL?XML=<>...</>&user=123456&password=123456
但是当我尝试创建curl调用时,它说它是一个无效的XML请求。
我尝试在POSTFIELDS中使用用户和密码。我还尝试在$ trial ='&amp; user = 123456&amp; password = 123456'之前将它们设置为变量,但似乎仍无法使其工作。
$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);
答案 0 :(得分:1)
这是错误的:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode($xml).urlencode('&user=123456&password=123456'));
urlencode只有变量名和值(或者只有你知道变量名才好的值):
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode($xml).'&user=' . urlencode('123456').'&password=' . urlencode('123456'));