Php-graph-sdk: Keep getting Stream returned an empty response

Created on 13 Sep 2016  路  12Comments  路  Source: facebookarchive/php-graph-sdk

No matter what I try I keep getting the error 'Stream returned an empty response.' I've created new apps on Facebook and it never fails. When I put in my localhost for a url, it works. When I put in my url, it doesn't.

Most helpful comment

Actually, facebook use curl or file_get_contents function to request an access token, it checks if curl is enabled or not, if curl is enabled on server, it tries to make request using curl otherwise it use file_get_contents to make request and get response. In my case, both allow_url_fopen as well as curl was disabled on server so i was getting the following error:

Facebook SDK returned an error: Stream returned an empty response

So, in order to get this fixed, you need to enable curl or allow_url_fopen on server .

The following code in file /facebook/src/Facebook/FacebookClient.php helped me to trace the issue:

public function detectHttpClientHandler()
{
return function_exists('curl_init') ? new FacebookCurlHttpClient() : new FacebookStreamHttpClient();
}

All 12 comments

Hey @dadixon! Can you provide the snippets of code that are causing this issue?

require_once('vendor/autoload.php');

if(!session_id()) {
session_start();
}

$fb = new FacebookFacebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.7',
]);

$helper = $fb->getRedirectLoginHelper();

try {
$accessToken = $helper->getAccessToken();
} catch(FacebookExceptionsFacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}

Here's the page with the login button code

`require_once('vendor/autoload.php');

if(!session_id()) {
session_start();
}

$fb = new FacebookFacebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.7',
]);

$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'public_profile']; // Optional permissions
$loginUrl = $helper->getLoginUrl('http://myurl/fb-test.php', $permissions);`

Should I not use the php sdk?

I had a similar problem which made me lose a lot of time.

Facebook SDK stop the php script (return an empty response by browser) very often.

The solution is that the new format of "ternary operator" is poorly supported on some versions of PHP.

You can find more information on that, but the solution is to change in every class:

$var1 = $var1?: $var2;

by (usually):

$var1 = $var1 ? $var1 : $var2;

I made a modified version that works perfectly for me: https://uploadfiles.io/8ec40

For Facebook Developer SDK, I have PHP version 5.4.45-1~dotdeb+7.1

This SDK doesn't work on nginx

I also have similar error using php 5.6.24 and php 7.0

@flexeo don't you want to make PR?

But with php 7.0 it's still not working :(

That exception will be thrown if file_get_contents() fails. It sounds like there's a networking issue in the servers that are throwing that exception. An E_WARNING will also be raised so check the error logs to see if the server is unable to resolve https://graph.facebook.com. I know some servers have TLS misconfigured which might cause this kind of issue.

Actually, facebook use curl or file_get_contents function to request an access token, it checks if curl is enabled or not, if curl is enabled on server, it tries to make request using curl otherwise it use file_get_contents to make request and get response. In my case, both allow_url_fopen as well as curl was disabled on server so i was getting the following error:

Facebook SDK returned an error: Stream returned an empty response

So, in order to get this fixed, you need to enable curl or allow_url_fopen on server .

The following code in file /facebook/src/Facebook/FacebookClient.php helped me to trace the issue:

public function detectHttpClientHandler()
{
return function_exists('curl_init') ? new FacebookCurlHttpClient() : new FacebookStreamHttpClient();
}

Make sure the packages php curl packages are installed
In Ubuntu: "apt install php-curl php7.1-curl"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SammyK picture SammyK  路  8Comments

sr-expert picture sr-expert  路  5Comments

igor84 picture igor84  路  8Comments

mohsenshakibafar picture mohsenshakibafar  路  4Comments

klausbreyer picture klausbreyer  路  10Comments