2016-02-26 74 views
2

根據UPS開發人員工具包文檔「跟蹤Web服務開發人員指南 - 2016年1月4日」的最新版本,可以通過REST/JSON訪問跟蹤API。但是,PDF文檔中關於JSON方面的信息很少,無論是在PDF中還是在Interwebs上。我無法弄清楚我的生活是否有正確的身體可以通過,並且每次我得到一個「」請求不完整或操作沒有定義。重新提交前檢查錯誤。「錯誤返回。在XML/SOAP方面有很多年的信息,但我很想在這個特定的項目上使用JSON。任何人都有任何關於他們API的新功能的經驗,以及JSON主體應該是什麼樣子? JSON驗證正確,但顯然不是UPS正在尋找的東西。UPS Tracking API - JSON body格式?

{ 
 
\t "UPSSecurity": { 
 
\t \t "UsernameToken": { 
 
\t \t \t "Username": " userHere", 
 
\t \t \t "Password": " passHere" 
 
\t \t } 
 
\t }, 
 
\t "ServiceAccessToken": { 
 
\t \t "AccessLicenseNumber": "licNoHere" 
 
\t }, 
 
\t "Request": { 
 
\t \t "RequestOption": "15" 
 
\t }, 
 
\t "InquiryNumber": "1Z12345E0291980793", 
 
\t "TrackingOption": "02" 
 
}

回答

3

發佈的答案,由UPS支持回答我自己的問題。

您會後下面的JSON作爲主體內容爲以下API端點:

https://onlinetools.ups.com/json/Track

{ 
 
    "Security": { 
 
    "UsernameToken": { 
 
     "Username": "ups_username_here", 
 
     "Password": "ups_password_here" 
 
    }, 
 
    "UPSServiceAccessToken": { 
 
     "AccessLicenseNumber": "ups_access_license_number_here" 
 
    } 
 
    }, 
 
    "TrackRequest": { 
 
    "Request": { 
 
     "RequestAction": "Track", 
 
     "RequestOption": "activity" 
 
    }, 
 
    "InquiryNumber": "1Z202Y36A898759591" 
 
    } 
 
}

+0

但是你怎麼做擺在首位的要求? – djack109

+0

@ djack109我在答案中添加了API端點和POST方法。 – Dan

+0

謝謝你。但是,您如何才能將數據傳輸到該網址。如果POST數據是表單字段名稱,則GET(URL)數據是什麼查詢字符串? – djack109

0

這裏是我用來做PHP代碼請求,使用cURL:

<?php 

$url = 'https://wwwcie.ups.com/rest/Track'; // TEST 
// $url = 'https://onlinetools.ups.com/rest/Track'; // PROD 

$ups_array = array(
    'UPSSecurity' => array(
     'UsernameToken' => array(
      'Username' => 'ups_username_here', 
      'Password' => 'ups_password_here' 
     ), 
     'ServiceAccessToken' => array(
      'AccessLicenseNumber' => 'ups_access_license_number_here' 
     ) 
    ), 
    'TrackRequest' => array(
     'Request' => array(
      'RequestOption' => 1, 
      'TransactionReference' => array(
       'CustomerContext' => 'Test 001' 
      ) 
     ), 
     'InquiryNumber' => 'ups_package_tracking_number_here' //'1ZE1XXXXXXXXXXXXXX' 
    ) 
); 

$params = json_encode($ups_array); // TURN THE ARRAY INTO JSON 

$results_json = scrape_page($url, $params); // POST TO THE API 

$results = json_decode($results_json, true); // DECODE THE JSON AND TURN IT INTO AN ARRAY 

print "<PRE><FONT COLOR=RED>"; print_r($results); print "</FONT></PRE>"; 

然後,對於實際捲曲功能,我有這樣的:

function scrape_page($url, $params) { 

    $headers = array(); 
    $headers[] = 'Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept'; 
    $headers[] = 'Access-Control-Allow-Methods: POST'; 
    $headers[] = 'Access-Control-Allow-Origin: *'; 
    $headers[] = 'Content-Type: application/json'; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 45); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
    $response = curl_exec($ch); 

    // CHECK TO SEE IF WE GOT AN ERROR 
    // IF SO, FORMAT IT LIKE THIS ::28::Operation timed out afterseconds 
    if ((curl_errno($ch)) && (curl_errno($ch) != 0)) { 
     $response = "::".curl_errno($ch)."::".curl_error($ch); 
    } 

    // SEND THE RESPONSE BACK TO THE SCRIPT 
    return $response; 

}