SOAP无法连接到主机PHP

时间:2015-04-12 17:02:00

标签: php api soap ups

我正在尝试连接到UPS API以检索信息,但我无法使其正常工作。我正在使用此代码:

  //die(phpinfo());
  //Configuration
  $access = "ACCESS NUMBER";
  $userid = "USERNAME";
  $passwd = "PASSWORD";
  $wsdl = "wsdl/Track.wsdl"; //wsdl path
  $operation = "ProcessTrack";
  $endpointurl = 'https://wwwcie.ups.com/webservices/Track'; //https://onlinetools.ups.com/ups.app/xml/Track or https://wwwcie.ups.com/webservices/Track
  $outputFileName = "XOLTResult.xml";

  function processTrack()
  {
      //create soap request
    $req['RequestOption'] = '15';
    $tref['CustomerContext'] = 'Add description here';
    $req['TransactionReference'] = $tref;
    $request['Request'] = $req;
    $request['InquiryNumber'] = TRACKING NUMBER;
    $request['TrackingOption'] = '02';

    //echo "Request.......\n";
    print_r($request);
    //echo "\n\n";
    return $request;
  }

  try
  {
    $mode = array
    (
         'soap_version' => 'SOAP_1_1',  // use soap 1.1 client
         'trace' => 1
    );

    // initialize soap client
    $client = new SoapClient($wsdl , $mode);

    //set endpoint url
    $client->__setLocation($endpointurl);


    //create soap header
    $usernameToken['Username'] = $userid;
    $usernameToken['Password'] = $passwd;
    $serviceAccessLicense['AccessLicenseNumber'] = $access;
    $upss['UsernameToken'] = $usernameToken;
    $upss['ServiceAccessToken'] = $serviceAccessLicense;

    $header = new SoapHeader('http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','UPSSecurity',$upss);
    $client->__setSoapHeaders($header);


    //get response
    $resp = $client->__soapCall($operation ,array(processTrack()));

    //get status
    //echo "Response Status: " . $resp->Response->ResponseStatus->Description ."\n";

    //save soap request and response to file
    $fw = fopen($outputFileName , 'w');
    fwrite($fw , "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" . $client->__getLastResponse() . "\n");
    fclose($fw);
    //echo "<br> Request:<br>";
    //print_r("<?xml version=\"1.0\" encoding=\"UTF-8\" \n" . $client->__getLastResponse() . "\n");
    header('Content-type: text/xml');
    echo $client->__getLastResponse();
  }
  catch(Exception $ex)
  {
      print_r($ex);
/*
    header('Content-type: text/xml');
    echo "<xml>Error</xml>";
*/
  }

我得到的错误是:

Array ( [Request] => Array ( [RequestOption] => 15 [TransactionReference] => Array ( [CustomerContext] => Add description here ) ) [InquiryNumber] => [TRACKING NUMBER] [TrackingOption] => 02 ) SoapFault Object ( [message:protected] => Could not connect to host [string:Exception:private] => [code:protected] => 0 [file:protected] => /Applications/XAMPP/xamppfiles/htdocs/kestrak/php/ups.php [line:protected] => 58 [trace:Exception:private] => Array ( [0] => Array ( [function] => __doRequest [class] => SoapClient [type] => -> [args] => Array ( [0] => [CREDENTIALS]Add description here[TRACKING NUMBER] [1] => https://wwwcie.ups.com/webservices/Track [2] => http://onlinetools.ups.com/webservices/TrackBinding/v2.0 [3] => 1 [4] => 0 ) ) [1] => Array ( [file] => /Applications/XAMPP/xamppfiles/[DIRECTORY] [line] => 58 [function] => __soapCall [class] => SoapClient [type] => -> [args] => Array ( [0] => ProcessTrack [1] => Array ( [0] => Array ( [Request] => Array ( [RequestOption] => 15 [TransactionReference] => Array ( [CustomerContext] => Add description here ) ) [InquiryNumber] => [TRACKING NUMBER] [TrackingOption] => 02 ) ) ) ) ) [previous:Exception:private] => [faultstring] => Could not connect to host [faultcode] => HTTP )

我刚从网站上下载了UPS API。在我的phpinfo()上,启用了soap。奇怪的是,当我使用c9.io时,它工作得非常好,但目前在我的本地主机上,我无法正常工作。 所以我最后得到的是一个xml文件,它输出使用跟踪号找到的结果。

2 个答案:

答案 0 :(得分:1)

您需要在本地PHP安装中启用CURL。

答案 1 :(得分:-1)

UPS正在升级所有基于Web的应用程序(包括UPS API)的通信安全协议。 请与您公司的IT部门或开发团队联系,以确保当前使用的任何安全协议都符合TLS 1.2要求。如果启用了Java,则必须是1.7或更高版本才能使用TLS 1.2。

如果在发布到API时遇到间歇性的连接问题,我们建议在您的应用程序中实现重试逻辑,因为它应在下次尝试时立即缓解。

如果无法这样做,请修改端点URL以使用temp-onlinetools.ups.com作为域。这是一个临时环境,可供因升级而遇到连接问题的用户使用。如果此建议的更改解决了此问题,则由于此临时环境,您将需要修改解决方案以使用TLS v1.2安全协议或更高版本连接到API。如果您选择使用此环境,请提供您希望符合TLS的日期。