@codebard
I am using a v2 client, PHP v7.1
My code as below.
$oauth_client = new Patreon\OAuth($client_id, $client_secret);
$tokens = $oauth_client->get_tokens($_POST['code'], $redirect_uri);
$access_token = $tokens['access_token'];
$refresh_token = $tokens['refresh_token'];
$api_client = new Patreon\API($access_token);
$api_client->api_return_format = 'array';
$patron_response = $api_client->fetch_user();
if ($patron_response['errors'])
{
var_dump($api_client);
//var_dump($patron_response);
}
else
{
$name = $patron_response['data']['attributes']['full_name'];
echo $name;
}
My code for API to fetch user, there is no change in this from the patreon-php provided code.
public function fetch_user() {
// Fetches details of the current token user.
return $this->get_data("identity?include=memberships&fields".urlencode("[user]")."=email,first_name,full_name,image_url,last_name,thumb_url,url,vanity,is_email_verified&fields".urlencode("[member]")."=currently_entitled_amount_cents,lifetime_support_cents,last_charge_status,patron_status,last_charge_date,pledge_relationship_start");
}
public function get_data( $suffix, $args = array() ) {
// Construct request:
$api_request = $this->api_endpoint . $suffix;
// This identifies a unique request
$api_request_hash = md5( $this->access_token . $api_request );
// Check if this request exists in the cache and if so, return it directly - avoids repeated requests to API in the same page run for same request string
if ( !isset( $args['skip_read_from_cache'] ) ) {
if ( isset( $this->request_cache[$api_request_hash] ) ) {
return $this->request_cache[$api_request_hash];
}
}
// Request is new - actually perform the request
$ch = $this->__create_ch($api_request);
$json_string = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
// don't try to parse a 500-class error, as it's likely not JSON
if ( $info['http_code'] >= 500 ) {
return $this->add_to_request_cache($api_request_hash, $json_string);
}
// don't try to parse a 400-class error, as it's likely not JSON
if ( $info['http_code'] >= 400 ) {
return $this->add_to_request_cache($api_request_hash, $json_string);
}
// Parse the return according to the format set by api_return_format variable
if( $this->api_return_format == 'array' ) {
$return = json_decode($json_string, true);
}
if( $this->api_return_format == 'object' ) {
$return = json_decode($json_string);
}
if( $this->api_return_format == 'json' ) {
$return = $json_string;
}
// Add this new request to the request cache and return it
return $this->add_to_request_cache($api_request_hash, $return);
}
private function __create_ch($api_request) {
// This function creates a cURL handler for a given URL. In our case, this includes entire API request, with endpoint and parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ( $this->api_request_method != 'GET' AND $this->curl_postfields ) {
curl_setopt( $ch, CURLOPT_POSTFIELDS, $this->curl_postfields );
}
// Set the cURL request method - works for all of them
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $this->api_request_method );
// Below line is for dev purposes - remove before release
// curl_setopt($ch, CURLOPT_HEADER, 1);
$headers = array(
'Authorization: Bearer ' . $this->access_token,
'User-Agent: Patreon-PHP, version 1.0.2, platform ' . php_uname('s') . '-' . php_uname( 'r' ),
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return $ch;
}