Page 1 of 2

HELP for file_get_contents

Posted: Sat Feb 02, 2013 9:27 pm
by kimcorros7
Good day. Newbie here. After doing some research, I can't find the solution to my problem.

Here is the part of the config.php

Code: Select all

if($_SESSION['USERID'] == "")
	{
		$A = $config['FACEBOOK_APP_ID'];
		$B = $config['FACEBOOK_SECRET'];
		define('FACEBOOK_APP_ID', $A);
		define('FACEBOOK_SECRET', $B);
		STemplate::assign('FACEBOOK_APP_ID',$A);
		STemplate::assign('FACEBOOK_SECRET',$B);
		
		function get_facebook_cookie($app_id, $application_secret) {
		  $args = array();
		  parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
		  ksort($args);
		  $payload = '';
		  foreach ($args as $key => $value) {
			if ($key != 'sig') {
			  $payload .= $key . '=' . $value;
			}
		  }
		  if (md5($payload . $application_secret) != $args['sig']) {
			return null;
		  }
		  return $args;
		}
		
		$code = $_REQUEST['code'];
		if($code != "")
		{
			$my_url = $config['baseurl']."/";
			$token_url = "https://graph.facebook.com/oauth/access_token?"
			. "client_id=" . $A . "&redirect_uri=" . urlencode($my_url)
			. "&client_secret=" . $B . "&code=" . $code;
			$response = @file_get_contents($token_url);
			$params = null;
			parse_str($response, $params);
			$graph_url = "https://graph.facebook.com/me?access_token=" 
			. $params['access_token'];
			$user = json_decode(file_get_contents($graph_url));
			$fname = htmlentities(strip_tags($user->name), ENT_COMPAT, "UTF-8");
			$femail = htmlentities(strip_tags($user->email), ENT_COMPAT, "UTF-8");
			
			$query="SELECT USERID FROM members WHERE email='".mysql_real_escape_string($femail)."' limit 1";
			$executequery=$conn->execute($query);
			$FUID = intval($executequery->fields['USERID']);
But this is what I get:

Warning: file_get_contents(): couldn't connect to server in /home/*****/public_html/include/config.php on line 254
Warning: file_get_contents(https://graph.facebook.com/me?access_token=): failed to open stream in /home/*****/public_html/include/config.php on line 254

Thanks. If you need to see more files, please do tell me. If you want to access my webhosting file manager, pm me.
If inappropriate, please delete this thread admin.

Re: HELP for file_get_contents

Posted: Sun Feb 03, 2013 12:01 am
by twinedev
Best guess would be that the server doesn't allow you to use URLs in file functions such as fopen and file_get_contents.

See the information under "filename" here http://www.php.net/manual/en/function.f ... parameters

Re: HELP for file_get_contents

Posted: Sun Feb 03, 2013 1:06 am
by kimcorros7
Thanks for the reply. There's something I changed
I added @ before fil_get_content and the warning was gone.
But it still doesn't log you in the site using Facebook.
Here is the website url: http://www.hahapinoy.com
Now try to login using your facebook and you will be redirected back to the homepage and still not logged in.

Re: HELP for file_get_contents

Posted: Sun Feb 03, 2013 2:38 am
by requinix
kimcorros7 wrote:I added @ before fil_get_content and the warning was gone.
That's all that it did: hid the warning. The problem is still there and the code is still not working.

Don't use @s unless you know exactly what you're doing. For the record, I can only think of one and only one circumstance where using it is a good idea (and this isn't it).

Re: HELP for file_get_contents

Posted: Sun Feb 03, 2013 2:51 am
by kimcorros7
Thanks Master. I'm doing more research now. Sorry for posting here.

Re: HELP for file_get_contents

Posted: Sun Feb 03, 2013 10:16 am
by twinedev
put the following in the script:

var_dump((bool)ini_get('allow_url_fopen'));

If it comes back false, it is a server setting preventing you from doing it, and in that case, contact your host.

-Greg

Re: HELP for file_get_contents

Posted: Sun Feb 03, 2013 5:01 pm
by kimcorros7
Thank you for the reply... Is that line supposed to turn on the allow_url_fopen? I guess it is enabled already...
Image

Re: HELP for file_get_contents

Posted: Mon Feb 04, 2013 8:55 am
by Eric!
Are you able to successfully use file_get_contents on other HTTPS links? You might not have the https wrapper installed on your sever.

What is the output of this on your server:

Code: Select all

<?php
$wrap = stream_get_wrappers();
echo '<pre>openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $wrap) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $wrap) ? 'yes':'no', "\n";
echo 'wrappers: ', print_r($wrap,true);
echo '</pre>'
?>
Should look something like:
[text]openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: Array
(
[0] => https
[1] => ftps
[2] => compress.zlib
[3] => compress.bzip2
[4] => php
[5] => file
[6] => glob
[7] => data
[8] => http
[9] => ftp
[10] => phar
[11] => zip
[12] => ssh2.shell
[13] => ssh2.exec
[14] => ssh2.tunnel
[15] => ssh2.scp
[16] => ssh2.sftp
)
[/text]

Re: HELP for file_get_contents

Posted: Mon Feb 04, 2013 2:24 pm
by kimcorros7
thank you for trying to help. I'll try this out and post the output.

Re: HELP for file_get_contents

Posted: Mon Feb 04, 2013 2:27 pm
by kimcorros7
Here's the output sir.

openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: Array
(
[0] => compress.zlib
[1] => compress.bzip2
[2] => tftp
[3] => ftp
[4] => telnet
[5] => dict
[6] => ldap
[7] => ldaps
[8] => http
[9] => https
[10] => ftps
[11] => scp
[12] => sftp
[13] => php
[14] => file
[15] => glob
[16] => data
[17] => phar
[18] => zip
[19] => rar
)

seems like it's enabled.

Re: HELP for file_get_contents

Posted: Mon Feb 04, 2013 9:42 pm
by Eric!
Then I have to ask the obvious, does the link reported in the warning actually work?

Re: HELP for file_get_contents

Posted: Mon Feb 04, 2013 9:56 pm
by kimcorros7
I'm sorry. I don't get your question... sorry, I'm Asian :(

Re: HELP for file_get_contents

Posted: Mon Feb 04, 2013 11:14 pm
by Eric!
Have you verified that the link in your warning:

[text]Warning: file_get_contents(): couldn't connect to server in /home/*****/public_html/include/config.php on line 254
Warning: file_get_contents(https://graph.facebook.com/me?access_token=): failed to open stream in /home/*****/public_html/include/config.php on line 254
[/text]

Is the URL that you want to get content from? It might not exist and facebook could be redirecting you to some other page which file_get_contents won't follow by default.

If you do have an invalid URL facebook responds with a header like the following:
[text]< HTTP/1.1 400 Bad Request
< Access-Control-Allow-Origin: *
< Cache-Control: no-store
< Content-Type: text/javascript; charset=UTF-8
< Expires: Sat, 01 Jan 2000 00:00:00 GMT
< Pragma: no-cache
< WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "An active access token must be used to query information about the current user."
< X-FB-Rev: 728554
< X-FB-Debug: OnIrfW+ynuVP8Y072AzweLT6+vRhZcxE4gho9KUC0sQ=
< Date: Tue, 05 Feb 2013 05:10:35 GMT
< Connection: keep-alive
< Content-Length: 140
[/text]

This header is probably too complex for file_get_contents to report properly...but I'm am just making a guess at your problem based on your posts. I think there is probably something wrong with your URL. Either the token or missing parameters.

Re: HELP for file_get_contents

Posted: Mon Feb 04, 2013 11:21 pm
by kimcorros7
Ok. I'm really going crazy. I guess I need to find another way. After doing some research, I found this: http://stackoverflow.com/questions/1083 ... entication

He solved this problem by shifting to PHP SDK (I really don't have any idea what that is... haha ^_^ )
Now what I want is to translate my codes just like her into something which will use PHP SDK.
I guess if someone can translate it for me, I would really be happy. This is my dream btw. To build a website for my countrymen here in Philippines. Thanks guys! I love you and I love this very supporting community! :D

Re: HELP for file_get_contents

Posted: Mon Feb 04, 2013 11:36 pm
by Eric!
You should look at using curl instead of file_get_contents. It's more flexible and has debugging options for solving complex network connections.