2015-04-12 158 views
2

我試圖連接到UPS API來檢索信息,但我無法得到它的工作。我使用此代碼:SOAP無法連接到主機PHP

//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()上,肥皂已啓用。奇怪的是,當我使用c9.io時,它工作得很好,但是目前在我的本地主機上,我無法正常工作。 所以我最後得到的應該是一個xml文件,輸出使用跟蹤號碼找到的結果。

回答

1

您需要在本地PHP安裝中啓用CURL。

+0

我有同樣的問題,但我沒有看到我的php.ini文件中的cURL選項,所以我不能取消分號如果它不是在哪裏被發現的分號。有任何想法嗎? – Supplement

+0

您可能需要安裝捲曲。 apt-get install php5-curl /etc/init.d/apache2 restart 或soap可能未啓用。檢查你的phpinfo()和ini – andyknas

+0

我在什麼目錄中添加它?提前致謝? – Supplement