2016-10-07 23 views
0
$whmcsUrl = "https://test.etravelsmart.com/etsAPI/api/getStations?"; 
// Admin username and password 
$username = "888888888"; 
$password = "**********"; 
// Set post values 
$postfields = array(
    'username' => $username, 
    'password' => md5($password), 
    'action' => 'GetClients', 
    'responsetype' => 'json', 
); 
$arrContextOptions=array(
    "ssl"=>array(
     "verify_peer"=>false, 
     "verify_peer_name"=>false, 
    ), 
); 

// Call the API 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $whmcsUrl); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields)); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
$response = curl_exec($ch); 
if (curl_error($ch)) { 
    die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch)); 
} 
curl_close($ch); 
// Attempt to decode response as json 
$jsonData = json_decode($response, true); 
// Dump array structure for inspection 
var_dump($jsonData); 

上記のプログラムはNULLを返します。 JSON形式は次のようになります。PHPを使用してREST APIのJSON URLを解析する方法は?

JSONはこのリンクの内側に配置されますhttps://test.etravelsmart.com/etsAPI/api/getStations?。このJSON URLに接続してそのデータをURLから解析する方法は?

{ 
    "stationList":[ 
     {"stationName":"10 dol","stationId":-1}, 
     {"stationName":"Chennai","stationId":2666} 
    ], 
    "apiStatus":{"success":true,"message":"SUCCESS"} 
} 
+0

あなたが要求を実行するときにエラーコードをチェックしましたか?ほとんどの場合、APIにリクエストに問題がある場合は、HTTPエラーコードが返され、APIのドキュメントを参照してください。 – CristianHG

+0

返信いただきありがとうございます。私のAPIドキュメントhttp://partners.etravelsmart.com/bus/busApi.htmあなたは私を助けることができますか? – shiny

答えて

0
 i got connected with api..my code is.. 
     <?php 
      $ch = curl_init(); 
      $url="http://test.etravelsmart.com/etsAPI/api/getStations"; 
      $username="888888888"; 
      $password='**********'; 
      curl_setopt($ch, CURLOPT_URL, $url); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
      curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); 
      $output = curl_exec($ch); 
      curl_close($ch); 


      $someArray = json_decode($output, true); 
      foreach($someArray as $stationList => $array){ 
      //echo "$stationList <br />"; 
      foreach($array as $name => $val){ 
       //echo "$name <br />"; 
      foreach($val as $sname => $value){ 
      echo "StationName:$value <br />"; 

     } 
      } 
     } 
1

あなたは認可を設定していないと思います。

curl_setopt($httpRequest, CURLOPT_HTTPHEADER, array('Authorization: ' . [AUTH_TOKEN], 'Content-Type: application/json')); 
関連する問題