PHP SOAP -无法连接到主机错误消息(皇家邮政运输API集成)


PHP SOAP - Could not connect to host error message (Royal Mail Shipping API integration)

我正在设置初始的皇家邮政航运API集成,并有以下PHP代码(注:我仍在等待皇家邮政的一些凭据,但此代码应该至少仍然连接&给我某种形式的认证错误。

我在本地运行了PHPINFO(),可以确认CURL &已启用OpenSSL。不太确定还有什么原因会导致这种情况……什么好主意吗?

我已经关闭了Windows防火墙,URL为http://localhost/royalmail/index.php

当我在浏览器中运行这个时,我得到了错误-这发生在代码块中大约一半的try块中。

Could not connect to host

下面的代码

<?php
//phpinfo();    
// AWAITING FROM ROYAL MAIL..
$api_password = "xxxx";
$api_username = "dispatch@company.com";
$api_application_id = "xxxxxx";
$api_service_type = "D";
$api_service_code = "SD1";
$api_service_format = "";
$api_certificate_passphrase = 'xxxxxxxxxxxx';
$api_service_enhancements = "";
// setting up the $data object
$data = new ArrayObject();
$data->order_tracking_id = "";
$data->shipping_name = "John";
$data->shipping_company = "AA";
$data->shipping_address1 = "23, Hurst Avenue"; 
$data->shipping_address2 = "";
$data->shipping_town = "London";
$data->shipping_postcode = "E1";
$data->order_tracking_boxes = "0";
$data->order_tracking_weight = "1500";    
$time = gmdate('Y-m-d'TH:i:s');
$created = gmdate('Y-m-d'TH:i:s'Z');
$nonce = mt_rand();
$nonce_date_pwd = pack("A*",$nonce) . pack("A*",$created) . pack("H*", sha1($api_password));
$passwordDigest = base64_encode(pack('H*',sha1($nonce_date_pwd)));
$ENCODEDNONCE = base64_encode($nonce);

$soapclient_options = array(); 
$soapclient_options['cache_wsdl'] = 'WSDL_CACHE_NONE'; 
$soapclient_options['local_cert'] = 'certs/CA2+Company+John-Bloggs+RM10001790+usr.p12';
$soapclient_options['passphrase'] = $api_certificate_passphrase;
$soapclient_options['trace'] = true;
$soapclient_options['ssl_method'] = 'SOAP_SSL_METHOD_SSLv3';
$soapclient_options['location'] = 'https://api.royalmail.com/shipping/onboarding';
//launch soap client
$client = new SoapClient('SAPI/ShippingAPI_V2_0_8.wsdl', $soapclient_options);
$client->__setLocation($soapclient_options['location']);
//headers needed for royal mail
$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-000">
              <wsse:Username>'.$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 );
//$header = new SoapHeader( 'SAPI/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Security', $HeaderObject );
$client->__setSoapHeaders($header);
//build the request
$request = array(
    'integrationHeader' => array(
        'dateTime' => $time,
        'version' => '1.0',
        'identification' => array(
            'applicationId' => $api_application_id,
            'transactionId' => $data->order_tracking_id
        )
    ),
    'requestedShipment' => array(
                                'shipmentType' => array('code' => 'Delivery'),
                                'serviceOccurence' => '1',
                                'serviceType' => array('code' => $api_service_type),
                                'serviceOffering' => array('serviceOfferingCode' => array('code' => $api_service_code)),
                                'serviceFormat' => array('serviceFormatCode' => array('code' => $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*1000) //weight of each individual item
                                                             )
                                                                )
                                                )
                                )               
);

//if any enhancements, add it into the array
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' => 'https://api.royalmail.com/shipping/onboarding') );
} catch (Exception $e) {
    //catch the error message and echo the last request for debug
    echo $e->getMessage(); 
    echo "REQUEST:'n" . $client->__getLastRequest() . "'n";
    die;
}
//check for any errors
if(isset($response->integrationFooter->errors)) { 
    $build = "";
    //check it wasn't a single error message
    if(isset($response->integrationFooter->errors->error->errorCode)) { 
        $build .= $output_error->errorCode.": ".$output_error->errorDescription."<br/>"; 
    } else {
        //loop out each error message, throw exception will be added ehre
        foreach($response->integrationFooter->errors->error as $output_error) { 
            $build .= $output_error->errorCode.": ".$output_error->errorDescription."<br/>";
        }
    }
    echo $build; die;
}
print_r($response);
echo "REQUEST:'n" . $client->__getLastRequest() . "'n";
die;
?>

错误发生的地方…

try { 
    $response = $client->__soapCall( 'createShipment', array($request), array('soapaction' => 'https://api.royalmail.com/shipping/onboarding') );
} catch (Exception $e) {
    //catch the error message and echo the last request for debug
    echo $e->getMessage(); 
    echo "REQUEST:'n" . $client->__getLastRequest() . "'n";
    die;
}

更新

我还没有创建*。pem文件用于此-我在指南中阅读了这一点。我创建了三个*。Pem文件(cacert. Pem)。pem, mycert。pem,Mykey.pem),但我不知道现在该怎么处理它们?

pkcs# 12 [http://en.wikipedia.org/wiki/PKCS_12]是从1996年开始的原始行业标准,用于安全传输证书和私钥(类似于加密的zip存档),并由各种应用程序和操作系统直接支持。这提供了一种标准化的"单一文件"方式来安全地传递证书、它的私钥和CA的公共签名证书的副本。

Linux和BSD上的标准web服务器是Apache,它期望所有的证书和密钥都是Base64编码版本的CER/DER x509v3二进制证书格式和RSA/DSA密钥。

在Linux上使用以下OpenSSL工具集命令从pkcs# 12文件(mycert.p12)中提取组成部分很容易。

% openssl pkcs12 -in mycert。P12 -cacerts -nokeys -out cacerts .pem

% openssl pkcs12 -in mycert。P12 -clcerts -nokeys out mycert.pem

% openssl pkcs12 -in mycert。P12 -nocerts -nodes -out mykeypem

"cacert。pem"文件包含CA的公开签名。通常,它被添加到系统范围的"可信CA"文件中(例如/etc/ssl/certs/ca-certificates)。CRT等)或可以直接引用的应用程序使用的文件本身。需要此证书来正式验证由同一CA颁发的任何证书。

通常是"mycert. net"。pem文件中包含pem编码的x509v3格式的"客户端SSL"证书。这用于在建立SSL/TLS连接时对服务器进行客户端身份验证。

"mykey。pem"文件包含链接到客户端证书的(现在)未加密的pem编码的RSA 2048位私钥。

应用程序在建立与应用程序和环境相关的SSL网络连接时如何传递已颁发的客户端SSL证书,但它本质上需要访问"mycert. net"answers"mycert. net"。Pem"answers"mykey"。Pem"文件,或者在某些情况下,包含证书和密钥的单个组合文件。

与皇家邮政的SOAP连接,无法连接到主机我使用的代码在我的答案,它的工作,如果你一直有代码的问题打电话给皇家邮政,并要求在实时服务器测试,一些定时器他们有问题与登录。