php soap 复杂请求 上海资信 金融p2p
要点
1 soap 链接 https的接口的时候 dorequest 方法需要重写
2 需要需要登录的话 dorequest 也要加入cookie
3 注意要加入wsdl规范 new soapclient('wsdl.txt',options)
wsdl.txt 从soap接口服务器下载
问题:
1 群友(php群 50194090)问soap接口的问题,,上海资信soap 。比较复杂的接口 需要登录 ,登录后输入网址,得到soap 接口的真实地址
2 、
先登录
$url = 'https://.shanghai-cis.com.cn/+web+/index.html';
$post_data = array(
'tgroup' =>'',
'next' =>'',
'tgcookieset' =>'',
'username'=>'aaa',
'password'=>'bbb',
'Login'=>'登录',
);
$headers = array(
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding:gzip, deflate",
"Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
"Connection:keep-alive",
"Cookie:webx=; weblogin=1; web_state=; csc_next=; weblogin=1; webLang=en",
"Host:.shanghai-cis.com.cn",
"Referer:https://.shanghai-cis.com.cn/+CSCOE+/logon.html",
"User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:39.0) Gecko/20100101 Firefox/39.0"
);
$link = curl($url,$headers,$post_data);
//var_dump($link);
$reg = '/web=\s*([^;]+);/is';
echo $link;
if (preg_match_all($reg,$link['header'],$p))
$cookiestr = implode(';',$p[1]);
function curl($url,$headers=array(),$post_data=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$errmsg = curl_error($ch);
$pos = strpos($response,"\r\n\r\n");
return array(
'code'=>curl_getinfo($ch,CURLINFO_HTTP_CODE),
'header'=>substr($response,0,$pos),
'body'=>substr($response,$pos+4),
'error'=>$errmsg
);
//curl_close($ch);
}
链接 soap
$context = stream_context_create( array (
'ssl' => array (
'verify_peer' => false,
'allow_self_signed' => true
),
));
$options['stream_context'] = $context;
$client = new SoapClientAuth('wsdl.txt', array(
'location' => 'https://vbbb/batchcredit?wsdl', // 设置server路径
'uri' => 'https://.ddd/batchcredit?wsdl',
'login' => 'ccc', // HTTP auth login
'password' => 'ddd', // HTTP auth password
'trace'=>true,
'targetNamespace'=>'http://webservice.creditreport.p2p.sino.com/',
'stream_context'=>$context
));
$client->__setCookie('web',$cookiestr);
var_dump($client);
返回方法,和测试方法
$param1 = array(
'orgcode'=>'Q100000000',
'secret'=>'bbbbb',
'plate'=>'1',
'certtype'=>'0',
'certno'=>'111',
'name'=>'李亚',
'reason'=>'06',
'createtype'=>'0'
);
var_dump($client->__getFunctions());
//var_dump($client->__soapCall());
$ret = $client->queryCredit($param1);
.
*/
/**
* SoapClientAuth
* The interface and operation of this class is identical to the PHP SoapClient class (http://php.net/manual/en/class.soapclient.php)
* except this class will perform HTTP authentication for both SOAP messages and while downloading WSDL over HTTP and HTTPS.
* Provide the options login and password in the options array of the constructor.
*
* @author tc
* @copyright Copyright (C) 2011 tc software
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @link http://php.net/manual/en/class.soapclient.php
* @link http://tcsoftware.net/
*/
class SoapClientAuth extends SoapClient{
public $Username = NULL;
public $Password = NULL;
/**
*
* @param string $wsdl
* @param array $options
*/
function SoapClientAuth($wsdl, $options = NULL)
{
@stream_wrapper_unregister('https');
@stream_wrapper_unregister('http');
stream_wrapper_register('https', 'streamWrapperHttpAuth');
stream_wrapper_register('http', 'streamWrapperHttpAuth');
if($options)
{
$this->Username = $options['login'];
streamWrapperHttpAuth::$Username = $this->Username;
$this->Password = $options['password'];
streamWrapperHttpAuth::$Password = $this->Password;
}
parent::SoapClient($wsdl, ($options?$options:array()));
@stream_wrapper_restore('https');
@stream_wrapper_restore('http');
}
function __doRequest($request, $location, $action, $version) {
$headers = array(
'User-Agent: PHP-SOAP',
'Content-Type: text/xml; charset=utf-8',
'SOAPAction: "' . $action . '"',
'Content-Length: ' . strlen($request),
'Expect: 100-continue',
'Connection: Keep-Alive'
);
$this->__last_request_headers = $headers;
$ch = curl_init($location);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_FAILONERROR, FALSE);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $this->Username . ':' . $this->Password);
global $cookiestr;
curl_setopt($ch, CURLOPT_COOKIE,'web='.$cookiestr);
//curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_CERTINFO, TRUE);
$response = curl_exec($ch);
if(($info = curl_getinfo($ch)) && $info['http_code']==200)
return $response;
else if($info['http_code']==401)
throw new Exception ('Access Denied', 401);
else if(curl_errno($ch)!=0)
{
throw new Exception(curl_error($ch), curl_errno($ch));
}else
print_r($info);
echo $info['http_code'];
throw new Exception('Error', $info['http_code']);
}
}
class streamWrapperHttpAuth
{
public static $Username = NULL;
public static $Password = NULL;
private $path = NULL;
private $position = 0;
private $buffer = NULL;
private $curlHandle = NULL;
public function stream_close()
{
if($this->curlHandle)
curl_close ($this->curlHandle);
}
public function stream_open($path, $mode, $options, &$opened_path)
{
$this->path = $path;
$response = $this->postRequest($this->path);
$this->buffer = ($response!==FALSE?$response:NULL);
$this->position = 0;
return $response!==FALSE;
}
public function stream_eof()
{
return $this->position>strlen($this->buffer);
}
public function stream_flush()
{
$this->position = 0;
$this->buffer = NULL;
}
public function stream_read($count)
{
if($this->buffer)
{
$data = substr($this->buffer, $this->position, $count);
$this->position += $count;
return $data;
}
return FALSE;
}
public function stream_write($data)
{
return ($this->buffer?TRUE:FALSE);
}
public function stream_seek($offset, $whence = SEEK_SET)
{
switch($whence)
{
case SEEK_SET:
$this->position = $offset;
break;
case SEEK_CUR:
$this->position += $offset;
break;
case SEEK_END:
$this->position = strlen($this->buffer) + $offset;
break;
}
return TRUE;
}
public function stream_tell()
{
return $this->position;
}
public function stream_stat()
{
return array('size' => strlen($this->buffer));
}
public function url_stat($path, $flags)
{
$response = $this->postRequest($path);
return array('size' => strlen($response));
}
protected function postRequest($path, $authType = CURLAUTH_ANY)
{
$this->curlHandle = curl_init($path);
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->curlHandle, CURLOPT_FOLLOWLOCATION, TRUE);
if(streamWrapperHttpAuth::$Username)
{
curl_setopt($this->curlHandle, CURLOPT_HTTPAUTH, $authType);
curl_setopt($this->curlHandle, CURLOPT_USERPWD, streamWrapperHttpAuth::$Username . ':' . streamWrapperHttpAuth::$Password);
}
//curl_setopt($this->curlHandle, CURLOPT_SSLVERSION, 3);
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, 2);
$response = curl_exec($this->curlHandle);
if(($info = curl_getinfo($this->curlHandle)) && $info['http_code']==200)
{
if(curl_errno($this->curlHandle)==0)
{
return $response;
}else
throw new Exception(curl_error($this->curlHandle), curl_errno($this->curlHandle));
}else if($info['http_code']==401)
{ // Attempt NTLM Auth only, CURLAUTH_ANY does not work with NTML
if($authType!=CURLAUTH_NTLM)
return $this->postRequest($path, CURLAUTH_NTLM);
else
{
throw new Exception ('Access Denied', 401);
}
}else if(curl_errno($this->curlHandle)!=0)
{
throw new Exception(curl_error($this->curlHandle), curl_errno($this->curlHandle));
}else
throw new Exception('Error', $info['http_code']);
return FALSE;
}
}
?>