Home > Uncategorized > Get a user’s profile picture from their Email address in #PHP using AvatarAPI

Get a user’s profile picture from their Email address in #PHP using AvatarAPI

The new Avatar API “v2” has code examples for cUrl (Command Line), Ruby, Python and Node, but PHP was overlooked, so here is a code example to help users get their PHP code up and running;

<?php
$email = "jenny.jones@gmail.com";


$json = '{ 
    "username" : "your-username-here",
    "password" : "yout-password-here",
    "email" : "$email"
  }'; 

$json = eval('return "' . addslashes($json) . '";');

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://avatarapi.com/v2/api.aspx',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_SSL_VERIFYPEER => false, 
  CURLOPT_POSTFIELDS => $json
));

$response = curl_exec($curl);

if (curl_errno($curl)) {
	// Optional (but recommended) error handling.
    $error_msg = curl_error($curl);
	echo $error_msg;
}

echo $response;

curl_close($curl);


?>

Note that you can remove the line CURLOPT_SSL_VERIFYPEER – I just had a local issue with SSL certs.

The output will look something like this;

{
  "Name": "",
  "Image": "https://lh3.googleusercontent.com/a-/AOh14GgoCfW3iuHPprK4x75MmdXtZ7387FvWVGt_GB5Y4A",
  "Valid": true,
  "City": "",
  "Country": "",
  "IsDefault": true
}
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: