title: Inconsistent error with Facebook Graph API in PHP - Failed to connect to graph.facebook.com port 443: Connection timed out
body: <p>I am integrating a Facebook login option on a site I'm working on and I've run into a bit of an issue when making calls to Facebook's API in PHP. The actual error I get is the following:</p>
  
  <blockquote>
    <p>Failed to connect to graph.facebook.com port 443: Connection timed out</p>
  </blockquote>
  
  <p>I've verified that the timeout value on the curl calls is correct, I tried checking the connection directly from the server's command prompt via SSH (pings correctly, the port seems to be open and processes are listening on it, etc). However, using a simple curl snippet I found online by others who had similar issues, I managed to test it easily and it seems like the issues is intermittent/inconsistent: sometimes it works flawlessly and quickly, and sometimes it loads for a few seconds and fails with the above error. </p>
  
  <p>I highly doubt that the error is on Facebook's side, but I can't figure out what it is I'm doing wrong. I've worked with the Facebook SDK in PHP before and it's the first time I see this error.</p>
  
  <p>Has anyone else faced this problem before and fixed it? </p>
  
  <p>Quick note: it's the first time I work with Facebook in a project based on Symfony - don't think it's relevant in this case but throwing it out there just in case.</p>
  
  <p>Relevant snippets:</p>
  
  <pre><code>$fb = new \Facebook\Facebook(['app_id' =&gt; '[withheld]',
                                        'app_secret' =&gt; '[withheld]',
                                        'default_graph_version' =&gt; 'v2.10',]);
  $fb-&gt;setDefaultAccessToken($accessToken); # Access token obtained from Facebook Login in JS, passed in post data.
  
  try {
      $basic_info_response = $fb-&gt;get('/me?fields=id,first_name,last_name,email,website');
  
      if ($with_picture)
          $profile_picture_response = $fb-&gt;get('/me/picture?width=720&amp;height=720');
  
      if ($with_friends)
          $friends_response = $fb-&gt;get('/me/friends');
  }
  catch(\Facebook\Exceptions\FacebookResponseException $e) {
      echo 'Graph returned an error: ' . $e-&gt;getMessage();
      exit;
  }
  catch(\Facebook\Exceptions\FacebookSDKException $e) {
      echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
      exit;
  }
  </code></pre>
  
  <p>In this code, it fails at any of the $fb->get() calls - not always the same one, sometimes on the first one, sometimes on the picture one, sometime on the friends one.</p>
  
  <p><strong>[Update]</strong></p>
  
  <p>The error is still happening, on around 90% of my calls. I tried to do curl calls directly on the server via SSH (<code>curl -v graph.facebook.com</code>) and got the two following results: </p>
  
  <pre><code>* Rebuilt URL to: graph.facebook.com/
  *   Trying 31.13.91.2...
  * TCP_NODELAY set
  *   Trying 2a03:2880:f01b:1:face:b00c:0:1...
  * TCP_NODELAY set
  * Immediate connect fail for 2a03:2880:f01b:1:face:b00c:0:1: Network is unreachable
  * Connected to graph.facebook.com (31.13.91.2) port 80 (#0)
  &gt; GET / HTTP/1.1
  &gt; Host: graph.facebook.com
  &gt; User-Agent: curl/7.50.2
  &gt; Accept: */*
  &gt; 
  &lt; HTTP/1.1 400 Bad Request
  &lt; WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api"
  &lt; Access-Control-Allow-Origin: *
  &lt; Pragma: no-cache
  &lt; Cache-Control: no-store
  &lt; x-fb-rev: 3274377
  &lt; Content-Type: application/json; charset=UTF-8
  &lt; x-fb-trace-id: A2OYZzFP3v8
  &lt; facebook-api-version: v2.4
  &lt; Expires: Sat, 01 Jan 2000 00:00:00 GMT
  &lt; Vary: Accept-Encoding
  &lt; X-FB-Debug: z9FEtq3Rlh8LyFn6pOIBZ5ZMCX+TY1jUD7iZ7ZRZ8/YGAsi035TbCP3qdBzqxDryvjJhKoKxnbAdvcxY/7r3Vg==
  &lt; Date: Mon, 04 Sep 2017 19:51:40 GMT
  &lt; Transfer-Encoding: chunked
  &lt; Connection: keep-alive
  &lt; 
  * Curl_http_done: called premature == 0
  * Connection #0 to host graph.facebook.com left intact
  </code></pre>
  
  <p>and</p>
  
  <pre><code>* Rebuilt URL to: graph.facebook.com/
  *   Trying 31.13.91.2...
  * TCP_NODELAY set
  * Connected to graph.facebook.com (31.13.91.2) port 80 (#0)
  &gt; GET / HTTP/1.1
  &gt; Host: graph.facebook.com
  &gt; User-Agent: curl/7.50.2
  &gt; Accept: */*
  &gt; 
  &lt; HTTP/1.1 400 Bad Request
  &lt; WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api"
  &lt; Access-Control-Allow-Origin: *
  &lt; Pragma: no-cache
  &lt; Cache-Control: no-store
  &lt; x-fb-rev: 3274377
  &lt; Content-Type: application/json; charset=UTF-8
  &lt; x-fb-trace-id: BrIDaH8D8D5
  &lt; facebook-api-version: v2.4
  &lt; Expires: Sat, 01 Jan 2000 00:00:00 GMT
  &lt; Vary: Accept-Encoding
  &lt; X-FB-Debug: tYvsf7Nn2PVnHFkV40UUddjQGKzPl8XKfdNeiqu1CXZck5WuUUlcG9hoCAZQrOX93uS19m2JpAEu9DJ/YhSIeg==
  &lt; Date: Mon, 04 Sep 2017 19:54:54 GMT
  &lt; Transfer-Encoding: chunked
  &lt; Connection: keep-alive
  &lt; 
  * Curl_http_done: called premature == 0
  * Connection #0 to host graph.facebook.com left intact
  </code></pre>
  
  <p>Does anyone have any more information or possible explanations for this issue?</p>