cha*_*cha 4 php xml curl web-services
我使用PHP curl将XML请求发送到webservice并获得响应.我的代码如下.
$url = "https://path_to_service.asp";
try{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($xmlRequest));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$data = curl_exec($ch);
//convert the XML result into array
if($data === false){
$error = curl_error($ch);
echo $error;
die('error occured');
}else{
$data = json_decode(json_encode(simplexml_load_string($data)), true);
}
curl_close($ch);
}catch(Exception $e){
echo 'Message: ' .$e->getMessage();die("Error");
}
Run Code Online (Sandbox Code Playgroud)
我从第三方Web服务中得到这个错误.他们说请求的方式可能无效,XML代码没问题.
"XML load failed. [Invalid at the top level of the document.]"
Run Code Online (Sandbox Code Playgroud)
但我的问题是;
当使用XML的请求时,此代码是否正确?
例如. curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($xmlRequest));
设置发布字段时,无需设置发布字段变量.
例如. curl_setopt($ch, CURLOPT_POSTFIELDS, "xmlRequest=" . $xmlRequest);
谢谢.
cha*_*cha 13
我正在与其他人分享我的解决方案,这对其他人有帮助.
$url = "https://path_to_service.asp";
//setting the curl parameters.
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\""
);
try{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
// send xml request to a server
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
//convert the XML result into array
if($data === false){
$error = curl_error($ch);
echo $error;
die('error occured');
}else{
$data = json_decode(json_encode(simplexml_load_string($data)), true);
}
curl_close($ch);
}catch(Exception $e){
echo 'Message: ' .$e->getMessage();die("Error");
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
| 归档时间: |
|
| 查看次数: |
19260 次 |
| 最近记录: |