PHP通过curl调用asp.net的WebService问题

itfamily 2011-01-04 01:06:22
当执行以下代码时返回 Data at the root level is invalid错误,请问如何解决。


<?php
ini_set("display_errors", "On"); error_reporting(E_ALL);
header('Content-Type:text/html;charset= utf-8'); //发送头部信息,确认页面编码
$user = 'aaa';
$pass = 'a';

$curlPost="username=" . $user . '&pwd=' . $pass . '&submit=send'; //定义需要发送的字符串,关键字与它的值
$ch=curl_init(); //初始化一个curl
curl_setopt($ch,CURLOPT_URL,'http://example.com/myservice/service.asmx?WSDL&op=UserLogin'); //设置目标URL
curl_setopt($ch,CURLOPT_POST,1); //设置使用POST发送数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost); //设置POST数据内容为前面定义的字符串
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //设置是否输出返回结果(设置为不打印)
$data=curl_exec($ch); //执行请求并返回结果
curl_close($ch); //关闭CURL
echo $data; //输出结果
?>





<?xml version="1.0" encoding="utf-8" ?>
- <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <soap:Body>
- <soap:Fault>
- <soap:Code>
<soap:Value>soap:Receiver</soap:Value>
</soap:Code>
- <soap:Reason>
<soap:Text xml:lang="en">Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1.</soap:Text>
</soap:Reason>
<soap:Detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>
...全文
404 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
itfamily 2011-01-05
  • 打赏
  • 举报
回复
谢谢#9
ygc2022 2011-01-04
  • 打赏
  • 举报
回复
既然人家都已经给出 webservice了 为什么还 用CURL
xeqtrl982 2011-01-04
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8"?>前面不能有前导空格,否则就是无效格式

<?php

$requstxml=$xml = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<UserLogin xmlns="http://tempuri.org/">
<username>test</username>
<pwd>test</pwd>
</UserLogin>
</soap12:Body>
</soap12:Envelope>

EOT;

$ch=curl_init('http://218.57.143.249/svctest/service.asmx');
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$requstxml);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/soap+xml; charset=utf-8", "Content-length: ".strlen($requstxml)));
$data=curl_exec($ch);
curl_close($ch);
echo $data;

?>
foolbirdflyfirst 2011-01-04
  • 打赏
  • 举报
回复
请求格式如何生成看#3

ini_set("display_errors", "On"); error_reporting(E_ALL); 
header('Content-Type:text/html;charset= utf-8'); //发送头部信息,确认页面编码
$user = 'aaa';
$pass = 'a';

$curlPost = <<<post
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">
<soap:Header/>
<soap:Body>
<tem:UserLogin>
<!--Optional:-->
<tem:username>{$user}</tem:username>
<!--Optional:-->
<tem:pwd>{$pass}</tem:pwd>
</tem:UserLogin>
</soap:Body>
</soap:Envelope>
post;

$ch=curl_init('http://218.57.143.249/svctest/service.asmx'); //初始化一个curl
//curl_setopt($ch,CURLOPT_URL,''); //设置目标URL
curl_setopt($ch,CURLOPT_POST,true); //设置使用POST发送数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost); //设置POST数据内容为前面定义的字符串
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); //设置是否输出返回结果(设置为不打印)
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/soap+xml;charset=UTF-8;action="http://tempuri.org/UserLogin"'));
$data=curl_exec($ch); //执行请求并返回结果
curl_close($ch); //关闭CURL

echo $data; //输出结果
itfamily 2011-01-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xeqtrl982 的回复:]

错误很明显
<Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1>
你的<?xml version="1.0" encoding="utf-8" ?> 在哪???
下面是一个访问ASP.NET HTTPS的WEBSERVICE
PHP code
……
[/Quote]

我2楼已经贴出来了,<?xml version="1.0" encoding="utf-8" ?> 这段是有的


WebService地址:http://218。57。143。249/svctest/service.asmx
请帮我用curl测试一下,谢谢。
xeqtrl982 2011-01-04
  • 打赏
  • 举报
回复
错误很明显
<Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1>
你的<?xml version="1.0" encoding="utf-8" ?> 在哪???
下面是一个访问ASP.NET HTTPS的WEBSERVICE

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, Your Webserviceurl);
curl_setopt($ch, CURLOPT_PORT , 443);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestxml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/soap+xml; charset=utf-8", "Content-length: ".strlen($requestxml)));
$response = curl_exec($ch);
$statuscode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
switch($statuscode) {
判断状态码
}


而$requestxml应该是

$requestxml="<?xml version=\"1.0\" encoding=\"utf-8\"?>".
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"> ""
等等,以下就不写了



没办法,用curl提交就是这样,自行拼接XML串,自行获取HTTP状态,自行解析XML,嫌麻烦就用NUSOAP或SOAPUI来玩
foolbirdflyfirst 2011-01-04
  • 打赏
  • 举报
回复
?部分就表示需要的的参数值
foolbirdflyfirst 2011-01-04
  • 打赏
  • 举报
回复
下载个soapui用用,你soap请求和普通的表单提交是不一样的。
下面是我copy了你的wsdl,soapui load入以后生成的提交http header和body

POST http://example.com/myservice/service.asmx HTTP/1.1
Connection: close
Accept-Encoding: gzip,deflate
Content-Type: application/soap+xml;charset=UTF-8;action="http://tempuri.org/UserLogin"
User-Agent: Jakarta Commons-HttpClient/3.1
Host: example.com
Content-Length: 343

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">
<soap:Header/>
<soap:Body>
<tem:UserLogin>
<!--Optional:-->
<tem:username>?</tem:username>
<!--Optional:-->
<tem:pwd>?</tem:pwd>
</tem:UserLogin>
</soap:Body>
</soap:Envelope>
itfamily 2011-01-04
  • 打赏
  • 举报
回复
这是WSDL内容

<?xml version="1.0" encoding="utf-8" ?>
- <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
- <wsdl:types>
- <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
- <s:element name="UserLogin">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="username" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="pwd" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="UserLoginResponse">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="UserLoginResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
- <wsdl:message name="UserLoginSoapIn">
<wsdl:part name="parameters" element="tns:UserLogin" />
</wsdl:message>
- <wsdl:message name="UserLoginSoapOut">
<wsdl:part name="parameters" element="tns:UserLoginResponse" />
</wsdl:message>
- <wsdl:portType name="Service1Soap">
- <wsdl:operation name="UserLogin">
<wsdl:input message="tns:UserLoginSoapIn" />
<wsdl:output message="tns:UserLoginSoapOut" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="Service1Soap" type="tns:Service1Soap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="UserLogin">
<soap:operation soapAction="http://tempuri.org/UserLogin" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:binding name="Service1Soap12" type="tns:Service1Soap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="UserLogin">
<soap12:operation soapAction="http://tempuri.org/UserLogin" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="Service1">
- <wsdl:port name="Service1Soap" binding="tns:Service1Soap">
<soap:address location="http://example.com/myservice/service.asmx" />
</wsdl:port>
- <wsdl:port name="Service1Soap12" binding="tns:Service1Soap12">
<soap12:address location="http://example.com/myservice/service.asmx" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>



我这边运行soap用不了
amani11 2011-01-04
  • 打赏
  • 举报
回复
为什么不用SoapClient呢?


你给出真实……?WSDL地址,看看参数格式,或贴上来

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧