2017-02-25 25 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, 
]); 
相關問題