2015-09-02 21 views
1

以前我使用XML方式從外部API獲取數據。現在,內部公司政策命令使用JSON與API進行通信。我知道我應該發送哪些數據,以及我將如何返回數據,但我不知道如何使用JSON調用API。使用PHP中的參數調用JSON API(使用示例)

  • 我的PHP服務器是本地主機
  • 讓JSON API位於https://api.example.com/service-api並且我確定它可以正常工作。
  • 請求 { 「行動」: 「授權」, 「ARGS」:{ 「用戶名」: 「[email protected]」, 「密碼」: 「qpujy676」}}
  • 回調 {」狀態「:」 OK」,‘令牌’:‘8hsa77hh687y3sbdha7ah3dajknfa93’}

我發現的例子,但我不知道什麼是Http_client(),它是什麼角色,什麼都行。

$http = new Http_Client(); 
    $http->setUri('https://api.example.com/service-api'); 
    $postdata = array(
     'action' => 'authorization', 
     'args' => array(
      'username' => '[email protected]', 
      'password' => 'qpujy676', 
     ) 
    ); 
    if (CRYPT_KEY_API) { //if encrypted data 
     $postdata ['rand'] = md5(time() . rand(2303, 85500)); 
     $postdata = json_encode($postdata); 
     $postdata = Crypt::encrypt($postdata); 
    } else { 
     $postdata = json_encode($postdata); 
    } 
    $http->setRawData($postdata, 'application/json'); 
    $response = $http->request(POST); 
    if ($response->isSuccessful()) { 
     $responseData = $response->getBody(); 
     if (CRYPT_KEY_API) { //if encrypted data 
      $responseData = Crypt::decrypt($responseData); 
     } 
     $results = json_decode($responseData, true); 
    } else { 
     $error_message = "<p>Error</p>\n"; 
     $error_message .= "HTTP Status: " . $response->getStatus() . "\n"; 
     $error_message .= "HTTP Headers:\n"; 
     $responseHeaders = $response->getHeaders(); 
     foreach ($responseHeaders as $responseHeaderName => $responseHeaderValue) { 
      $error_message .= "$responseHeaderName: $responseHeaderValue\n"; 
     } 
     throw new Exception($error_message); 
    } 

實際上我需要僅是示例性http_client類解決我的問題:-)特別是

  • setRawData($ POSTDATA, '應用程序/ JSON')
  • 請求(POST)
  • getBody()

I creared method autorization_test(),t帽子據說應該用外部API來響應。據說,因爲它的輸出是:

警告: 的file_get_contents(https://api.example.com/service-api): 未能打開流:HTTP請求失敗! HTTP/1.1 404未找到在 C:\ XAMPP \ htdocs中\ prestashop1611 \模塊\ miniwa \ miniwa.php上線180 錯誤

public function authorization_test() 
    { 
     $domain = MINIWA_DOMAIN; 
     $username = '[email protected]'; 
     $password = '12345'; 

     $postData = array(
      'action' => 'authorization', 
      'args' => array(
       'domain' => $domain, 
       'username' => $username, 
       'password' => $password, 
      ), 
     ); 

     $postData = json_encode($postData); 
     //$postData = Crypt::encrypt($postData); 

     $context = stream_context_create(array(
      'http' => array(
      // http://www.php.net/manual/en/context.http.php 
      'method' => 'POST', 
      'header' => 'Content-Type: application/json', 
      'content' => $postData, 
      ) 
     )); 

     // Send the request 
     $response = file_get_contents('https://api.example.com/service-api', FALSE, $context); 

     // Check for errors 
     if($response === FALSE){ 
      die('Error'); 
     } 

     //$response= Crypt::decrypt($response); 

     // Decode the response 
     $responseData = json_decode($response, TRUE); 

     // send the output data to view 
     $smarty->assign(array(
      'json_decoded_output' => $responseData, 
     )); 

     return $smarty; 

爲什麼沒有正輸出?

+0

也許找一個用curl來代替的例子吧? – Jasen

+0

我可以在沒有http_client的情況下請求嗎?它有必要嗎? – Bejkrools

回答

0

API被禁用,現在是確定的,代碼是正確的。