title: Retrieving Facebook / Google+ / Linkedin profile picture having email address only
body: <h3>What I need</h3>
  
  <p>I need to automatically find &amp; download profile picture for user knowing his email address only. Originally, I focused on Facebook considering the amount of people actively using it. However, there seem to be no direct support from their API anymore.</p>
  
  <p>There was similar question here:
  <a href="https://stackoverflow.com/q/12104873/1168156">How to get a facebook user id from the login email address</a> which is quite outdated and current answers there are "it's deprecated" / "it's not possible"... 
  EDIT: I've found even better question: <a href="https://stackoverflow.com/q/5234651/1168156">Find Facebook user (url to profile page) by known email address</a> (where it is actually explained why and since when this feature isn't supported)</p>
  
  <h3>There must be a way...</h3>
  
  <p>What makes me think that this should be possible is that <strong><em>Spokeo</em></strong> is somehow doing it:
  <a href="http://www.spokeo.com/email-search/search?e=beb090303%40hotmail.com" rel="nofollow noreferrer">http://www.spokeo.com/email-search/search?e=beb090303%40hotmail.com</a> </p>
  
  <p>There are some services / APIs offering this kind of feature:<br>
  <a href="https://clearbit.com/" rel="nofollow noreferrer">Clearbit</a><br>
  <a href="https://pipl.com/dev" rel="nofollow noreferrer">Pipl</a><br>
  ...but I haven't found anything free.</p>
  
  <h3>Alternatives</h3>
  
  <p>If there is some workaround or different approach than using Facebook's API to achieve this, I would like to know. If Facebook is really completely hopeless here, then combination of these: <strong><em>Google+</em></strong>, <strong><em>Linkedin</em></strong> and/or <strong><em>Gravatar</em></strong> could do.</p>
  
  <hr>
  
  <h3>My first (original) attempt:</h3>
  
  <p>Once you have Facebook's username or user ID, it's easy to build URL to download the picture. So I was trying to look for Facebook's user IDs using emails with the <code>/search</code> Graph API:<br>
  <code>https://graph.facebook.com/search?q=beb090303@hotmail.com&amp;type=user&amp;access_token=TOKEN</code></p>
  
  <p>which unfortunatelly always ends with <em>"A user access token is required to request this resource."</em></p>
  
  <strong><em>Using FB PHP API + FB App ID &amp; Secret</em></strong>
  
  <p>I've also tried this: at first I retrieve <code>access_token</code> using app ID and secret and then I'm trying to use it as a part of <code>/search</code> request with <code>curl</code>:</p>
  
  <pre><code>function post_query_url($url, $data) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      $res = curl_exec($ch);
      curl_close($ch);
      return $res;
  }
  
  function get_query_url($url) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POST, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      $ret = curl_exec($ch);
      curl_close($ch);
      return $ret;
  }
  
  function get_retrieve_app_access_token($app_id, $secret) {
      $url = 'https://graph.facebook.com/oauth/access_token?client_id='.$app_id.'&amp;client_secret='.$secret.'&amp;grant_type=client_credentials';
      $res = get_query_url($url);
      if (!empty($res)) {
          $tokens = explode('=', $res);
          if (count($tokens) == 2)
              return $tokens[1];
      }
      return null;
  }
  
  function post_retrieve_app_access_token($app_id, $secret) {
      $url = 'https://graph.facebook.com/oauth/access_token';
      $data = 'client_id='.$app_id.'&amp;client_secret='.$secret.'&amp;grant_type=client_credentials';
      $res = post_query_url($url, $data);
      if (!empty($res)) {
          $tokens = explode('=', $res);
          if (count($tokens) == 2)
              return $tokens[1];
      }
      return null;
  }
  
  function get_id_from_email($email, $accessToken) {
      $url = 'https://graph.facebook.com/search?q='.urlencode($email).'&amp;type=user&amp;access_token='.$accessToken;
      $res = get_query_url($url);
      if (!empty($res)) {
          return $res;
      }
      return null;
  }
  
  echo 'Retrieving token...&lt;br&gt;';
  
  $token = post_retrieve_app_access_token('MY_APP_ID', 'SECRET');
  echo 'Retrieved token: ' . $token . '&lt;br&gt;';
  
  echo 'Retrieving user ID...&lt;br&gt;';
  $id = get_id_from_email('beb090303@hotmail.com', $token);
  echo 'Retrieved ID: ' . $id . '&lt;br&gt;';
  </code></pre>
  
  <p>outputs something like:</p>
  
  <pre><code>Retrieving token...
  Retrieved token: 367458621954635|DHfdjCnvO243Hbe1AFE3fhyhrtg
  Retrieving user ID...
  Retrieved ID: {"error":{"message":"A user access token is required to request this resource.","type":"OAuthException","code":102}}
  </code></pre>
  
  <h3>Other info</h3>
  
  <p>Since it's asking for "user access token", I've also tried to go to Facebook's Graph Explorer: <a href="https://developers.facebook.com/tools/explorer/" rel="nofollow noreferrer">https://developers.facebook.com/tools/explorer/</a>
  let it generate access token for me and queried:
  <code>search?q=beb090303@hotmail.com&amp;type=user&amp;debug=all</code>
  That one ends with:</p>
  
  <pre><code>{
    "error": {
      "message": "(#200) Must have a valid access_token to access this endpoint", 
      "type": "OAuthException", 
      "code": 200
    }
  }
  </code></pre>
  
  <p>...so Facebook seems kinda hopeless here.</p>