传递数组时,API返回1个配置文件数据

时间:2017-02-25 02:12:10

标签: php api curl

过去1天我一直坚持这一点,似乎无法让它发挥作用。当我将userIds数组传递给profilesearch时,我只能从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);
    }

}

1 个答案:

答案 0 :(得分:0)

我认为API可能需要另一种JSON格式。但我会建议 不要手工制作JSON字符串,而应使用json_encode来构建 JSON字符串,即:

$request = json_encode([
    'formNames' => ['Athlete Summary'],
    'userIds' => $userIds,
]);