HELP for file_get_contents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: HELP for file_get_contents

Post 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.

But I still think your fundamental problem is with your graph.facebook.com URL.
kimcorros7
Forum Newbie
Posts: 14
Joined: Sat Feb 02, 2013 9:11 pm

Re: HELP for file_get_contents

Post by kimcorros7 »

Hmm. I also encountered curl while researching. I also want to try it, but honestly I am not a programmer. I'm just here to seek help from those who are masters on PHP. Can you help me out on translating it to curl or PHP SDK?
kimcorros7
Forum Newbie
Posts: 14
Joined: Sat Feb 02, 2013 9:11 pm

Re: HELP for file_get_contents

Post by kimcorros7 »

Hello Eric. So far this is the code with curl function...

Code: Select all

function get_data_cURL($url)
{
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
function generateCode($length) 
{
	$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789";
    $code = "";
    $clen = strlen($chars) - 1;
    while (strlen($code) < $length) {
        $code .= $chars[mt_rand(0,$clen)];
    }
    return $code;
}
if($config['enable_fc'] == "1")
{
	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;
			$params = null;
			parse_str($response, $params);
			$graph_url = "https://graph.facebook.com/me?access_token=" 
			. $params['access_token'];
			$user = get_data_cURL($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']);
			if($FUID > 0)
			{									
				$query="SELECT USERID,email,username,verified, filter from members WHERE USERID='".mysql_real_escape_string($FUID)."' and status='1'";
				$result=$conn->execute($query);
				if($result->recordcount()>0)
				{
					$query="update members set lastlogin='".time()."', lip='".$_SERVER['REMOTE_ADDR']."' WHERE USERID='".mysql_real_escape_string($FUID)."'";
					$conn->execute($query);
					$_SESSION['USERID']=$result->fields['USERID'];
					$_SESSION['EMAIL']=$result->fields['email'];
					$_SESSION['USERNAME']=$result->fields['username'];
					$_SESSION['VERIFIED']=$result->fields['verified'];
					$_SESSION['FILTER']=$result->fields['filter'];
					$_SESSION['FB']="1";			
					header("Location:$config[baseurl]/");exit;
				}
			}
			else
			{
				$md5pass = md5(generateCode(5).time());
				
				if($fname != "" && $femail != "")
				{
					$query="INSERT INTO members SET email='".mysql_real_escape_string($femail)."',username='', password='".mysql_real_escape_string($md5pass)."', addtime='".time()."', lastlogin='".time()."', ip='".$_SERVER['REMOTE_ADDR']."', lip='".$_SERVER['REMOTE_ADDR']."', verified='1'";
					$result=$conn->execute($query);
					$userid = mysql_insert_id();
					if($userid != "" && is_numeric($userid) && $userid > 0)
					{
						$query="SELECT USERID,email,verified, filter from members WHERE USERID='".mysql_real_escape_string($userid)."'";
						$result=$conn->execute($query);
						
						$SUSERID = $result->fields['USERID'];
						$SEMAIL = $result->fields['email'];
						$SVERIFIED = $result->fields['verified'];
						$SFILTER = $result->fields['filter'];
						$_SESSION['USERID']=$SUSERID;
						$_SESSION['EMAIL']=$SEMAIL;
						$_SESSION['VERIFIED']=$SVERIFIED;
						$_SESSION['FILTER']=$SFILTER;
						$_SESSION['FB']="1";				
						header("Location:$config[baseurl]/connect.php");exit;
					}
				}
			}
		}
	}
	function getCurrentPageUrl()
	{
		 static $pageURL = '';
		 if(empty($pageURL)){
			  $pageURL = 'http';
			  if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')$pageURL .= 's';
			  $pageURL .= '://';
			  if($_SERVER['SERVER_PORT'] != '80')$pageURL .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
			  else $pageURL .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		 }
		 return $pageURL;
	} 
	if($_SESSION['USERNAME'] == "" && $_SESSION['FB'] == "1")
	{	
		$url = getCurrentPageUrl();
		$myurl = $config['baseurl']."/connect.php";
		$cssurl = $config['baseurl']."/css/connect.css";
		if(($url != $myurl) && ($url != $cssurl))
		{
			header("Location:$config[baseurl]/connect.php");exit;
		}
	}
}

But still I can't login with my Facebook account. It just returns me to the homepage with this url:
http://www.hahapinoy.com/?code=AQDwqnLn ... uxNoyl#_=_

Can you tell me what's wrong with the code above? Thanks. This is the only problem that I am facing now before I'm going to launch the site. Please help me master.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: HELP for file_get_contents

Post by Eric! »

For debugging curl do something like this

Code: Select all

function get_data_cURL($url,$fp_debug)
{
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            curl_setopt($ch, CURLOPT_VERBOSE, TRUE); // for debugging
            curl_setopt($ch, CURLOPT_STDERR, $fp_debug); //for debugging, need file pointer

  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
In your code, call it like this, and setup a debug file:

Code: Select all

           $fp_debug= fopen("debug.txt", "w"); // setup debug file -- make sure PHP and apache has permission to WRITE to this directory
// your other code goes here
// when you want to call your curl function pass the pointer to your "debug.txt" file
           $user=get_data_cURL($url,$fp_debug);
// other code goes here...
// if you need to call curl again
           $more_data=get_data_cURL($url,$fp_debug); // this will keep adding info to your debug.txt file
// when your routine is done, close the file
           fclose($fp_debug); //for debugging
Then look in the debug.txt and this will help you find what you are doing wrong when communicating with facebook.
kimcorros7
Forum Newbie
Posts: 14
Joined: Sat Feb 02, 2013 9:11 pm

Re: HELP for file_get_contents

Post by kimcorros7 »

In which part should I place those codes? Thank you I'll try this.
kimcorros7
Forum Newbie
Posts: 14
Joined: Sat Feb 02, 2013 9:11 pm

Re: HELP for file_get_contents

Post by kimcorros7 »

THANKS ERIC. This is what goes into the debug.txt
* <url> malformed

what should i do with this?
kimcorros7
Forum Newbie
Posts: 14
Joined: Sat Feb 02, 2013 9:11 pm

Re: HELP for file_get_contents

Post by kimcorros7 »

changing the $url to 'http://hahapinoy.com/login/' this is the result

* Connection #0 to host http://www.hahapinoy.com left intact
* Closing connection #0
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: HELP for file_get_contents

Post by Eric! »

The first error should be a clue that your URL is wrong.

And you're posting results from a different URL and that can't be the complete debug.txt file. If it is the complete debug.txt there is something wrong with your code. Post your full debug.txt and code.
Post Reply