Hello guys im trying to connect to royalmail shipping api SOAP but getting couldnt connect to host error…my code is below:the error is down the code…any help would be really appreciated!!
ini_set('default_socket_timeout', 120);
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
class royalmaillabelRequest
{
private $apiapplicationid = "xxx";
private $api_username = "xxxAPI"; //"rxxxxxAPI"
private $api_password = "xxx";
private $location_for_request = 'https://api.royalmail.com/shipping/onboarding'; //live 'https://api.royalmail.com/shipping' onbording 'https://api.royalmail.com/shipping/onboarding'
private $clientId='xxx';
private $clientSecret='xx';
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 = pack("A*",$nonce) . pack("A*",$created) . pack("H*", sha1($this->api_password));
$passwordDigest = base64_encode(pack('H*',sha1($nonce_date_pwd)));
$encodednonce = base64_encode($nonce);
//SET CONNECTION DETAILS
$soapclient_options['cache_wsdl'] = 'WSDL_CACHE_NONE';
$soapclient_options['stream_context']= stream_context_create(
array(
'http' =>
array(
'header' => implode(
"\r\n",
array(
'Accept: application/soap+xml',
'X-IBM-Client-Id: '.$this->clientId,
'X-IBM-Client-Secret: '.$this->clientSecret,
)
),
),
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
)
)
);
$soapclient_options['trace'] = 0;
$soapclient_options['exception'] = 1;
$soapclient_options['ssl_method'] = 'SOAP_SSL_METHOD_SSLv3';
$soapclient_options['location'] = $this->location_for_request;
$soapclient_options['soap_version'] = 'SOAP_1_2';
//launch soap client
$client = new SoapClient(dirname(__FILE__) . "/SAPI/ShippingAPI_V2_0_9.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->location_for_request) );
// echo "REQUEST:\n" . htmlentities($client->__getLastResponse()) . "\n";
break;
} catch (Exception $e) {
echo "<pre>";
print_r($e);
echo "</pre>";
if($e->detail->exceptionDetails->exceptionCode == "E0010" && $times <= 25){
sleep(1.5);
$times++;
continue;
}else{
echo $e->getMessage();
echo "<pre>";
print_r($e->detail);
echo "</pre>";
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;
}
}
$request = new royalmaillabelRequest();
$response = $request->CreateShippiment(
array(
'order_tracking_id'=>'123',
'shipping_name' => 'Mark',
'shipping_company' => 'AK boys and co.',
'shipping_address1' => 'Unit E1, Telford Road' ,
'shipping_address2' => '',
'shipping_town' => 'Bicester',
'shipping_postcode' => 'OX26 4LD',
'order_tracking_boxes'=>1,
'order_tracking_weight'=>500,
'api_service_type'=>'D',
'api_service_code'=>'SD1',
'api_service_format'=>''
));
error
SoapFault Object
(
[message:protected] => Could not connect to host
[string:Exception:private] =>
[code:protected] => 0
[file:protected] => /var/www/html/royalmail/index.php
[line:protected] => 123
[trace:Exception:private] => Array
(
[0] => Array
(
[function] => __doRequest
[class] => SoapClient
[type] => ->
[args] => Array
(
[0] =>
xxxxAPI
P7PRFGmMpVAHm7yxZRqhPpDHPsY=
ODA3NzE2MQ==
2016-03-29T08:50:12Z
2016-03-29T08:50:122RMG-API-G-01123Delivery1DSD12016-03-29MarkAK boys and co.Unit E1, Telford RoadBicesterOX26 4LD1g500
[1] => https://api.royalmail.com/shipping/onboarding
[2] => createShipment
[3] => 1
[4] => 0
)
)
[1] => Array
(
[file] => /var/www/html/royalmail/index.php
[line] => 123
[function] => __soapCall
[class] => SoapClient
[type] => ->
[args] => Array
(
[0] => createShipment
[1] => Array
(
[0] => Array
(
[integrationHeader] => Array
(
[dateTime] => 2016-03-29T08:50:12
[version] => 2
[identification] => Array
(
[applicationId] => xxxx
[transactionId] => 123
)
)
[requestedShipment] => Array
(
[shipmentType] => Array
(
[code] => Delivery
)
[serviceOccurrence] => 1
[serviceType] => Array
(
[code] => D
)
[serviceOffering] => Array
(
[serviceOfferingCode] => Array
(
[code] => SD1
)
)
[serviceFormat] => Array
(
[serviceFormatCode] => Array
(
[code] =>
)
)
[shippingDate] => 2016-03-29
[recipientContact] => Array
(
[name] => Mark
[complementaryName] => AK boys and co.
)
[recipientAddress] => Array
(
[addressLine1] => Unit E1, Telford Road
[addressLine2] =>
[postTown] => Bicester
[postcode] => OX26 4LD
)
[items] => Array
(
[item] => Array
(
[numberOfItems] => 1
[weight] => Array
(
[unitOfMeasure] => Array
(
[unitOfMeasureCode] => Array
(
[code] => g
)
)
[value] => 500
)
)
)
)
)
)
[2] => Array
(
[soapaction] => https://api.royalmail.com/shipping/onboarding
)
)
)
[2] => Array
(
[file] => /var/www/html/royalmail/index.php
[line] => 89
[function] => makerequest
[class] => royalmaillabelRequest
[type] => ->
[args] => Array
(
[0] => createShipment
[1] => Array
(
[integrationHeader] => Array
(
[dateTime] => 2016-03-29T08:50:12
[version] => 2
[identification] => Array
(
[applicationId] => RMG-API-G-01
[transactionId] => 123
)
)
[requestedShipment] => Array
(
[shipmentType] => Array
(
[code] => Delivery
)
[serviceOccurrence] => 1
[serviceType] => Array
(
[code] => D
)
[serviceOffering] => Array
(
[serviceOfferingCode] => Array
(
[code] => SD1
)
)
[serviceFormat] => Array
(
[serviceFormatCode] => Array
(
[code] =>
)
)
[shippingDate] => 2016-03-29
[recipientContact] => Array
(
[name] => Mark
[complementaryName] => AK boys and co.
)
[recipientAddress] => Array
(
[addressLine1] => Unit E1, Telford Road
[addressLine2] =>
[postTown] => Bicester
[postcode] => OX26 4LD
)
[items] => Array
(
[item] => Array
(
[numberOfItems] => 1
[weight] => Array
(
[unitOfMeasure] => Array
(
[unitOfMeasureCode] => Array
(
[code] => g
)
)
[value] => 500
)
)
)
)
)
)
)
[3] => Array
(
[file] => /var/www/html/royalmail/index.php
[line] => 225
[function] => CreateShippiment
[class] => royalmaillabelRequest
[type] => ->
[args] => Array
(
[0] => Array
(
[order_tracking_id] => 123
[shipping_name] => Mark
[shipping_company] => AK boys and co.
[shipping_address1] => Unit E1, Telford Road
[shipping_address2] =>
[shipping_town] => Bicester
[shipping_postcode] => OX26 4LD
[order_tracking_boxes] => 1
[order_tracking_weight] => 500
[api_service_type] => D
[api_service_code] => SD1
[api_service_format] =>
)
)
)
)
[previous:Exception:private] =>
[faultstring] => Could not connect to host
[faultcode] => HTTP
)