与英国皇家邮政SOAP连接,无法连接到主机 [英] SOAP connection with Royal Mail, Could not connect to host
本文介绍了与英国皇家邮政SOAP连接,无法连接到主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图连接到英国皇家邮政运输的API,但我收到了著名无法连接到主机。
$ api_password =****;
$ api_username =****;
$ api_application_id =****;
$ api_service_type =D;
$ api_service_ code =SD1;
$ api_service_format =;
$ api_certificate_passphrase =****;
$ =时间gmdate('Y-M-D \\ TH:我:S');
$创建= gmdate(Y-M-D \\ TH:ⅰ:■\\ Z');
$乱数= mt_rand();
$ nonce_date_pwd = XYZ(从样本复印件);
$ passwordDigest = ZYZ(从样本复印件);
$ EN codeDNONCE = ZYZ(复印件来自抽样); $ soapclient_options =阵列();
$ soapclient_options ['cache_wsdl'] ='WSDL_CACHE_NONE';
$ soapclient_options ['的local_cert'] =CA2 +飞溅+费利佩+ RM10001654 + usr.p12
$ soapclient_options ['密语'] = $ api_certificate_passphrase;
$ soapclient_options ['跟踪'] =真;
$ soapclient_options ['ssl_method'] ='SOAP_SSL_METHOD_SSLv3';
$ soapclient_options ['位置'] ='****';
//启动SOAP客户端
$客户端=新SoapClient的(SAPI / ShippingAPI_V2_0_8.wsdl,$ soapclient_options);
$客户 - > __的setLocation($ soapclient_options ['位置']);
(设置标题)
$ HeaderObject =新SoapVar($ HeaderObjectXML,XSD_ANYXML); //推SOAP头
$头=新SOAPHEADER('绿洲-200401-WSS-wssecurity型多功能1.0.xsd','安全',$ HeaderObject);
$客户 - > __ setSoapHeaders($头);
(设置请求的一部分)
如果($ api_service_enhancements!=){
$请求['requestedShipment'] ['serviceEnhancements'] =阵列('额外说明'=>阵列('serviceEnhancement code'=>阵列('code'=> $ api_service_enhancements)));
} //尝试拨打电话
尝试{
$响应= $客户 - > __的SOAPCall('createShipment',阵列($要求),阵列('的SOAPAction'=>'*** API链接***'));
}赶上(例外$ E){ //捕获错误消息,并回应了调试的最后一个请求
回声$ E->的getMessage();
回声的要求:\\ n。 $客户 - > __ getLastRequest()。 \\ n;
死;
}
它正确我设置的连接和本地证书的方式吗?
是我缺少的信息吗?
感谢和放大器;问候
解决方案
请按照我的最后code :)这一项工作是肯定的。即使在情况下,服务器是比济重试,欣赏。
块引用>< PHP
//ini_set('soap.wsdl_cache_enabled','1');
的ini_set('soap.wsdl_cache_enabled',0);
的ini_set('soap.wsdl_cache_ttl',0);
类royalmaillabelRequest
{
私人$ apiapplicationid =插入URS
私人$ api_password =插入URS
私人$ api_username =插入URS //rxxxxxAPI
私人$ api_certificate_passphrase =插入URS
私人$ locationforrequest ='https://api.royalmail.com/shipping/onboarding'; //活https://api.royalmail.com/shippingonbordinghttps://api.royalmail.com/shipping/onboarding
私人$ api_service_enhancements =; 私有函数preparerequest(){ //密码摘要
$ =时间gmdate('Y-M-D \\ TH:我:S');
$创建= gmdate(Y-M-D \\ TH:ⅰ:■\\ Z');
$乱数= mt_rand();
$ nonce_date_pwd = XYZ(从样本复印件);
$ passwordDigest = NYZ(从样本复印件);
$ EN codeDNONCE =(复印件来自抽样); // SET CONNECTION详情 $ soapclient_options =阵列();
$ soapclient_options ['cache_wsdl'] ='WSDL_CACHE_NONE';
$ soapclient_options ['stream_context'] = stream_context_create(
阵列('HTTP'=>
阵列(
PROTOCOL_VERSION'=>'1.0'
,'头'=> 连接:关闭
)
)
);
$ soapclient_options ['的local_cert'] =目录名(__ FILE__)。 /certificate.pem;
$ soapclient_options ['密码'] = $这个 - > api_certificate_passphrase;
$ soapclient_options ['跟踪'] =真;
$ soapclient_options ['ssl_method'] ='SOAP_SSL_METHOD_SSLv3';
$ soapclient_options ['位置'] = $这个 - > locationforrequest;
$ soapclient_options ['soap_version'] ='SOAP_1_1'; //启动SOAP客户端
$客户端=新SoapClient的(目录名(__ FILE__)/SAPI/ShippingAPI_V2_0_8.wsdl,$ soapclient_options);
$客户 - > __的setLocation($ soapclient_options ['位置']); //需要英国皇家邮政// D8D094Fd2716E3Es142588808s317头
$ HeaderObjectXML ='< WSSE:安全的xmlns:WSSE =http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd
的xmlns:WSU =http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd>
< WSSE:用户名令牌WSU:ID =用户名令牌-D8D094FC22716E3EDE14258880881317>
< WSSE:用户名>'。$这个 - > api_username。'< / WSSE:用户名>
&LT; WSSE:密码Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">'.$passwordDigest.'</wsse:Password>
&LT; WSSE:杜撰EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">'.$EN$c$cDNONCE.'</wsse:Nonce>
&LT; WSU:创建&GT;&LT; / WSU'$创建。':创建&GT;
&LT; / WSSE:用户名令牌&GT;
&LT; / WSSE:安全&GT;';
//推入头肥皂
$ HeaderObject =新SoapVar($ HeaderObjectXML,XSD_ANYXML);
//推SOAP头
$头=新SOAPHEADER('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd','安全',$ HeaderObject);
$客户 - &GT; __ setSoapHeaders($头);
返回$客户端; } 公共职能CreateShippiment($数据){ $请求= $这个 - &GT; buildCreateshippiment($的数据);
$型='createShipment'; 返回$这个 - &GT; makeRequest的($型,$请求);
}
公共职能PrintLabel($ shipmentNumber,$ order_tracking_id){ $ =时间gmdate('Y-M-D \\ TH:我:S');
$请求=阵列(
integrationHeader'=&GT;阵列(
'日期时间'=&GT; $时,
'版本'=&GT; '2',
'标识'=&GT;阵列(
'的applicationID'=&GT; $这个 - &GT; apiapplicationid,
值TransactionID=&GT; $ order_tracking_id
)
)
shipmentNumber'=&GT; $ shipmentNumber,
OUTPUTFORMAT'=&GT; PDF,
);
$型='printLabel';
$响应= $这个 - &GT; makeRequest的($型,$请求);
返回$响应 - &GT;标签; } 私有函数makeRequest的($型,$请求){ $客户端= $这个 - &GT; preparerequest();
$响应= FALSE;
$倍= 1; 而(真){ 尝试{
$响应= $客户 - &GT; __的SOAPCall($类型,数组($要求),阵列('的SOAPAction'=&GT; $这个 - &GT; locationforrequest)); //回声的要求:\\ n。ヶ辆($客户 - &GT; __ getLastResponse()获得)。 \\ n;
打破; }赶上(例外$ E){ 的print_r($ E);
如果($ E-GT&;&detail- GT; exceptionDetails-&GT;例外code ==E0010&放大器;&安培; $次&LT; = 25){ 睡眠(1.5);
$倍++;
继续; }其他{
回声$ E-&GT;的getMessage();
回声&LT; pre&gt;中;
的print_r($ E-GT&;细节);
回声$客户 - &GT; __ getLastResponse()获得;
回声的要求:\\ n。ヶ辆($客户 - &GT; __ getLastResponse()获得)。 \\ n;
打破; } } 打破;
}
返回$反应;
} 私有函数buildCreateshippiment($数据2){ $ =时间gmdate('Y-M-D \\ TH:我:S'); $数据=新ArrayObject的();
的foreach($ DATA2为$关键=&GT; $值)
{
$数据 - &GT; $键= $价值;
}
$请求=阵列(
integrationHeader'=&GT;阵列(
'日期时间'=&GT; $时,
'版本'=&GT; '2',
'标识'=&GT;阵列(
'的applicationID'=&GT; $这个 - &GT; apiapplicationid,
值TransactionID=&GT; $数据 - &GT; order_tracking_id
)
)
requestedShipment'=&GT;阵列(
shipmentType'=&GT;阵列('code'=&GT;'交货'),
serviceOccurrence'=&GT; 1,
'的serviceType'=&GT;阵列('code'=&GT; $数据 - &GT; api_service_type)
serviceOffering'=&GT;阵列('serviceOffering code'=&GT;阵列('code'=&GT; $数据 - &GT; api_service_ code)),
serviceFormat'=&GT;阵列('serviceFormat code'=&GT;阵列('code'=&GT; $数据 - &GT; api_service_format))
shippingDate'=&GT;日期('Y-M-D'),
recipientContact'=&GT;阵列('名'=&GT; $数据 - &GT; shipping_name,'complementaryName'=&GT; $数据 - &GT; shipping_company)
recipientAddress'=&GT;阵列('addressLine1'=&GT; $数据 - &GT; shipping_address1,'addressLine2'=&GT; $数据 - &GT; shipping_address2,'postTown'=&GT; $数据 - &GT; shipping_town,后code'=&GT ; $数据 - &GT; shipping_post code)
'项目'=&GT;阵列('项目'=&GT;阵列(
numberOfItems'=&GT; $数据 - &GT; order_tracking_boxes,
重量=&GT;阵列('unitOfMeasure'=&GT;阵列('unitOfMeasure code'=&GT;阵列('code'=&GT;'G'))
'值'=&GT; $数据 - &GT; order_tracking_weight,
)
)
)
//'签名'=&GT; 0, )
); 如果($数据 - &GT; api_service_enhancements == 6安培;&安培; $数据 - &GT; api_service_type == 1){
$请求['requestedShipment'] ['serviceEnhancements'] =阵列('额外说明'=&GT;阵列('serviceEnhancement code'=&GT;阵列('code'=&GT; $数据 - &GT; api_service_enhancements )));
} 返回$请求; }
}I'm trying to connect to Royal Mail shipping API, but I'm receiving the famous Could not connect to host.
$api_password = "****"; $api_username = "****"; $api_application_id = "****"; $api_service_type = "D"; $api_service_code = "SD1"; $api_service_format = ""; $api_certificate_passphrase = "****"; $time = gmdate('Y-m-d\TH:i:s'); $created = gmdate('Y-m-d\TH:i:s\Z'); $nonce = mt_rand(); $nonce_date_pwd = xyz(copy from sample); $passwordDigest = zyz(copy from sample); $ENCODEDNONCE = zyz(copy from sample); $soapclient_options = array(); $soapclient_options['cache_wsdl'] = 'WSDL_CACHE_NONE'; $soapclient_options['local_cert'] = "CA2+Splash+Felipe+RM10001654+usr.p12"; $soapclient_options['passphrase'] = $api_certificate_passphrase; $soapclient_options['trace'] = true; $soapclient_options['ssl_method'] = 'SOAP_SSL_METHOD_SSLv3'; $soapclient_options['location'] = '****'; //launch soap client $client = new SoapClient("SAPI/ShippingAPI_V2_0_8.wsdl", $soapclient_options); $client->__setLocation($soapclient_options['location']);
(setting header)
$HeaderObject = new SoapVar( $HeaderObjectXML, XSD_ANYXML ); //push soap header $header = new SoapHeader( 'oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Security', $HeaderObject ); $client->__setSoapHeaders($header);
(setting request part)
if($api_service_enhancements != "") { $request['requestedShipment']['serviceEnhancements'] = array('enhancementType' => array('serviceEnhancementCode' => array('code' => $api_service_enhancements))); } //try make the call try { $response = $client->__soapCall('createShipment', array($request), array('soapaction' => '***api-link***') ); } catch (Exception $e) { //catch the error message and echo the last request for debug echo $e->getMessage(); echo "REQUEST:\n" . $client->__getLastRequest() . "\n"; die; }
Is it correct the way I'm setting the connection and the local cert? Is any information I'm missing? Thanks & Regards
解决方案Follow my final code :) this one works for sure. Even have the retry in case the server is buzy, enjoy.
<?php //ini_set('soap.wsdl_cache_enabled', '1'); ini_set('soap.wsdl_cache_enabled',0); ini_set('soap.wsdl_cache_ttl',0); class royalmaillabelRequest { private $apiapplicationid = "insert urs"; private $api_password = "insert urs"; private $api_username = "insert urs"; //"rxxxxxAPI" private $api_certificate_passphrase = "insert urs"; private $locationforrequest = 'https://api.royalmail.com/shipping/onboarding'; //live 'https://api.royalmail.com/shipping' onbording 'https://api.royalmail.com/shipping/onboarding' private $api_service_enhancements = ""; private function preparerequest(){ //PASSWORD DIGEST $time = gmdate('Y-m-d\TH:i:s'); $created = gmdate('Y-m-d\TH:i:s\Z'); $nonce = mt_rand(); $nonce_date_pwd = xyz(copy from sample); $passwordDigest = nyz(copy from sample); $ENCODEDNONCE = (copy from sample); //SET CONNECTION DETAILS $soapclient_options = array(); $soapclient_options['cache_wsdl'] = 'WSDL_CACHE_NONE'; $soapclient_options['stream_context'] = stream_context_create( array('http'=> array( 'protocol_version'=>'1.0' , 'header' => 'Connection: Close' ) ) ); $soapclient_options['local_cert'] = dirname(__FILE__) . "/certificate.pem"; $soapclient_options['passphrase'] = $this->api_certificate_passphrase; $soapclient_options['trace'] = true; $soapclient_options['ssl_method'] = 'SOAP_SSL_METHOD_SSLv3'; $soapclient_options['location'] = $this->locationforrequest; $soapclient_options['soap_version'] = 'SOAP_1_1'; //launch soap client $client = new SoapClient(dirname(__FILE__) . "/SAPI/ShippingAPI_V2_0_8.wsdl", $soapclient_options); $client->__setLocation($soapclient_options['location']); //headers needed for royal mail//D8D094Fd2716E3Es142588808s317 $HeaderObjectXML = '<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> <wsse:UsernameToken wsu:Id="UsernameToken-D8D094FC22716E3EDE14258880881317"> <wsse:Username>'.$this->api_username.'</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">'.$passwordDigest.'</wsse:Password> <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">'.$ENCODEDNONCE.'</wsse:Nonce> <wsu:Created>'.$created.'</wsu:Created> </wsse:UsernameToken> </wsse:Security>'; //push the header into soap $HeaderObject = new SoapVar( $HeaderObjectXML, XSD_ANYXML ); //push soap header $header = new SoapHeader( 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Security', $HeaderObject ); $client->__setSoapHeaders($header); return $client; } public function CreateShippiment($data){ $request = $this->buildCreateshippiment($data); $type = 'createShipment'; return $this->makerequest($type, $request); } public function PrintLabel($shipmentNumber,$order_tracking_id){ $time = gmdate('Y-m-d\TH:i:s'); $request = array( 'integrationHeader' => array( 'dateTime' => $time, 'version' => '2', 'identification' => array( 'applicationId' => $this->apiapplicationid, 'transactionId' => $order_tracking_id ) ), 'shipmentNumber' => $shipmentNumber, 'outputFormat' => 'PDF', ); $type = 'printLabel'; $response = $this->makerequest($type, $request); return $response->label; } private function makerequest($type, $request){ $client = $this->preparerequest(); $response = false; $times = 1; while(true){ try { $response = $client->__soapCall( $type, array($request), array('soapaction' => $this->locationforrequest) ); // echo "REQUEST:\n" . htmlentities($client->__getLastResponse()) . "\n"; break; } catch (Exception $e) { print_r($e); if($e->detail->exceptionDetails->exceptionCode == "E0010" && $times <= 25){ sleep(1.5); $times++; continue; }else{ echo $e->getMessage(); echo "<pre>"; print_r($e->detail); echo $client->__getLastResponse(); echo "REQUEST:\n" . htmlentities($client->__getLastResponse()) . "\n"; break; } } break; } return $response; } private function buildCreateshippiment($data2) { $time = gmdate('Y-m-d\TH:i:s'); $data = new ArrayObject(); foreach ($data2 as $key => $value) { $data->$key = $value; } $request = array( 'integrationHeader' => array( 'dateTime' => $time, 'version' => '2', 'identification' => array( 'applicationId' => $this->apiapplicationid, 'transactionId' => $data->order_tracking_id ) ), 'requestedShipment' => array( 'shipmentType' => array('code' => 'Delivery'), 'serviceOccurrence' => 1, 'serviceType' => array('code' => $data->api_service_type), 'serviceOffering' => array('serviceOfferingCode' => array('code' => $data->api_service_code)), 'serviceFormat' => array('serviceFormatCode' => array('code' => $data->api_service_format)), 'shippingDate' => date('Y-m-d'), 'recipientContact' => array('name' => $data->shipping_name, 'complementaryName' => $data->shipping_company), 'recipientAddress' => array('addressLine1' => $data->shipping_address1, 'addressLine2' => $data->shipping_address2, 'postTown' => $data->shipping_town, 'postcode' => $data->shipping_postcode), 'items' => array('item' => array( 'numberOfItems' => $data->order_tracking_boxes, 'weight' => array( 'unitOfMeasure' => array('unitOfMeasureCode' => array('code' => 'g')), 'value' => $data->order_tracking_weight, ) ) ), //'signature' => 0, ) ); if($data->api_service_enhancements == 6 && $data->api_service_type == 1){ $request['requestedShipment']['serviceEnhancements'] = array('enhancementType' => array('serviceEnhancementCode' => array('code' => $data->api_service_enhancements))); } return $request; } }
这篇关于与英国皇家邮政SOAP连接,无法连接到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文