我希望有人可以帮助解决这个问题。我正试图通过UKMail提供的soap服务登录。到目前为止,我的代码看起来像这样:
<?php
$data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ser="http://www.UKMail.com/Services/Contracts/ServiceContracts"
xmlns:dat="http://www.UKMail.com/Services/Contracts/DataContracts">
<soapenv:Header/>
<soapenv:Body>
<ser:Login>
<ser:LoginWebRequest>
<dat:Username>xxx</dat:Username>
<dat:Password>xxx</dat:Password>
</ser:LoginWebRequest>
</ser:Login>
</soapenv:Body>
</soapenv:Envelope>';
$curl = new cURL();
$page = $curl->soap_post('https://api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl',$data);
echo $page;
class cURL {
var $headers;
var $user_agent;
var $compression;
var $cookie_file;
var $cookies;
var $proxy;
function cURL($cookies=TRUE, $cookie_file='cookies.txt', $compression='gzip,deflate', $proxy='') {
$this->headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$this->headers[] = 'Connection: Keep-Alive';
$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=windows-1251';
$this->user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)';
$this->compression=$compression;
$this->proxy=$proxy;
$this->cookies=$cookies;
$this->cookie_file=$cookie_file;
}
function soap_post($url,$data){
$headers = array(
'Content-type: text/xml; charset=utf-8',
'Content-Length: '.strlen($data),
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'SOAPAction: "http://www.UKMail.com/Services/IUKMAuthenticationService/Login"',
'Expect: '
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$out = curl_exec($ch);
return $out;
}
}
?>
运行上面的代码给了我一个没有设置为对象错误实例的对象,我不能在我的生活中找出我出错的地方。我尝试过使用PHP的SoapClient,但它在我的客户端生产服务器上运行不正常,所以我正在使用curl。
我忘了提到你在代码中看到的xml实际上是由UKMail自己提供的,如果这有什么不同。
错误: 对象引用未设置为对象的实例
答案 0 :(得分:0)
只需将wsdl文件的url插入创建xml请求的soapUI即可。所以我应对它创建的xml并在我的脚本中使用它并且它工作了!据我所知,我所写的内容与soapUI产生的唯一区别在于用户名和密码字段的顺序错误。
答案 1 :(得分:0)
我已经改变了你的一些代码并让它适合我,试一试
<?php
$data = "<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:ser='http://www.UKMail.com/Services/Contracts/ServiceContracts'
xmlns:dat='http://www.UKMail.com/Services/Contracts/DataContracts'>
<soapenv:Header/>
<soapenv:Body>
<ser:Login>
<ser:loginWebRequest>
<dat:Password>xxxxxxxxxxx</dat:Password>
<dat:Username>xxxxxxxxxxx</dat:Username>
</ser:loginWebRequest>
</ser:Login>
</soapenv:Body>
</soapenv:Envelope>";
$curl = new cURL();
echo $page = $curl->soap_post('https://api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl',$data);
class cURL {
function soap_post($url,$data){
$headers = array(
'Content-type: text/xml; charset=utf-8',
'Content-Length: '.strlen($data),
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'SOAPAction: "http://www.UKMail.com/Services/IUKMAuthenticationService/Login"',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$out = curl_exec($ch);
return $out;
}
}
?>