2017-02-25 2 views
1

私はこれを過去1日間執着しており、動作させられないようです。私がuserIdsの配列をprofilesearchに渡すと、APiから1つの結果しか返されません。配列が渡されたときにAPIが1つのプロファイルデータを返す

私は間違っていることを教えてください。

class Players{ 

    public $username = "username"; 
    public $password = "password"; 
    public $base_url = "https://www.example.com/api/v1"; 
    public $userIds = []; 

    //This is the first method that fires of to get the userId 
    public function usersearch(){ 
     $url = $this->base_url . '/usersearch?informat=json&format=json'; 
     $request = '{"identification":[]}'; 
     $username = $this->username; 
     $password = $this->password; 
     $ch = curl_init(); 
     curl_setopt_array($ch, array(
      CURLOPT_URL => $url, 
      CURLOPT_HEADER => false, 
      CURLOPT_HTTPHEADER => array("Content-Type: UTF-8"), 
      CURLOPT_USERPWD => "$username:$password", 
      CURLOPT_TIMEOUT => 60, 
      CURLOPT_CUSTOMREQUEST => "POST", 
      CURLOPT_POSTFIELDS => $request, 
      CURLOPT_RETURNTRANSFER => true, 
      CURLOPT_UNRESTRICTED_AUTH => true, 
      CURLOPT_FORBID_REUSE => false, 
      CURLOPT_SSL_VERIFYHOST => 0, 
      CURLOPT_SSL_VERIFYPEER => 0 
     )); 
     $response = curl_exec($ch); 
     $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
     if (curl_errno($ch)) { 
      var_dump(curl_error($ch), 'cURL Errors'); 
      curl_close($ch); 
     } else { 
      $result = array(); 
      $result = json_decode($response, true); 
      $result = $result['results'][0]['results']; 
      //Now that we have the list of users, loop over it and get their userId and save it in the userIds property as in array 
      foreach($result as $key => $item){ 
       $this->userIds[] = $item['userId']; 
      } 
     } 
     curl_close($ch); 
     //Let's go and grab the profile data 
     $this->profilesearch(); 
    } 

    public function profilesearch(){ 
     //Make a local variable of the property userIds which contains list of userIds in array format 
     $userIds = $this->userIds; 
     $userIds = implode(',', $userIds); //This becomes "101,239,240" 
     $url = $this->base_url . '/profilesearch?informat=json&format=json'; 
     $request = '{"formNames":["Athlete Summary"],"userIds":[' . $userIds . ']}'; //This becomes {"formNames":["Athlete Summary"],"userIds":[101,239,240]} 
     $username = $this->username; 
     $password = $this->password; 
     $ch = curl_init(); 
     curl_setopt_array($ch, array(
      CURLOPT_URL => $url, 
      CURLOPT_HEADER => false, 
      CURLOPT_HTTPHEADER => array("Content-Type: UTF-8"), 
      CURLOPT_USERPWD => "$username:$password", 
      CURLOPT_TIMEOUT => 60, 
      CURLOPT_CUSTOMREQUEST => "POST", 
      CURLOPT_POSTFIELDS => $request, 
      CURLOPT_RETURNTRANSFER => true, 
      CURLOPT_UNRESTRICTED_AUTH => true, 
      CURLOPT_FORBID_REUSE => false, 
      CURLOPT_SSL_VERIFYHOST => 0, 
      CURLOPT_SSL_VERIFYPEER => 0 
     )); 
     $response = curl_exec($ch); 
     $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
     if (curl_errno($ch)) { 
      var_dump(curl_error($ch), 'cURL Errors'); 
     } else { 
      $result = array(); 
      $result = json_decode($response, true); 
      var_dump($result); //I get only one user data 
     } 
     curl_close($ch); 
    } 

} 

答えて

0

おそらくAPIが別のJSON形式を想定していると思います。しかし、私はあなたが JSON文字列すなわちを構築する代わりにjson_encodeを使用する必要があります手でJSON文字列を作るない に推薦する。:

$request = json_encode([ 
    'formNames' => ['Athlete Summary'], 
    'userIds' => $userIds, 
]); 
関連する問題