Arnaud
Arnaud

Reputation: 750

Server can't file_get_contents on https

I'm trying to get my FB app access token using the following code:

$app_token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret 
. "&grant_type=client_credentials";
$response = file_get_contents($app_token_url);
$params = null;
parse_str($response, $params);

echo("This app's access token is: " . $params['access_token']);

It works fine from localhost but not from my server (connection times out). The openssl library is enabled according to phpinfo().

Update: the problem seems to occur on any https URL. allow_url_fopen is On.

Update 2: it seems like it is a firewall issue. I can't wget any https url when I log on the server via SSH. I've asked them to open port 443.

Upvotes: 0

Views: 3305

Answers (3)

Abdelhamed Mohamed
Abdelhamed Mohamed

Reputation: 11

<?php

$url = 'https://www.google.com.eg';

function curl_get_contents($url){
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
echo curl_get_contents($url);
?>

Upvotes: 1

user1068963
user1068963

Reputation:

Try Check your openssl can wrapper data or not in your server?

code to check:

<?php
$check = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'isload':'noload','<br>';
echo 'http: ', in_array('http', $check) ? 'ok':'no','<br>';
echo 'https: ', in_array('https', $check) ? 'ok':'no','<br>';
?>

if ok will get output:

openssl: isload
http: ok
https: ok

Upvotes: 2

Tommy Crush
Tommy Crush

Reputation: 2800

Try increasing the timeout limit:

<?php 
$context = stream_context_create(array( 
    'http' => array( 
        'timeout' => 60 
        ) 
    ) 
); 
file_get_contents("http://example.com/", 0, $context); 
?>

Upvotes: 0

Related Questions