简体   繁体   中英

I am able to fetch the data from facebook in json format but not able to insert in the database.

The code is working fine but i am not able to insert the user data in the mysql database.

<?php

$facebookAppAuthUrl = 'https://graph.facebook.com/oauth/access_token';
$facebookGraphUrl = 'https://graph.facebook.com';
$facebookClientId = ''; // Put your App Id here.
$facebookRedirectUrl = ''; // Redirect url same as passed before.
$facebookAppSecret = ""; // Put your App Secret here.

$code = $_GET['code'];

$url =$facebookAppAuthUrl."?client_id=".$facebookClientId
."&redirect_uri=".$facebookRedirectUrl
."&client_secret=".$facebookAppSecret
."&code=".$code;

$output = urlResponse($url);
$var = strtok($output, "&");
$ApCode = strtok($var, "=");
$ApCode = strtok("=");

//  This $ApCode will be used as a token to get user info from facebook.

$url = $facebookGraphUrl.'/me';
echo '<pre>';
$resposeObj = json_decode(processUrl($url,$ApCode));
var_dump($resposeObj);
echo '<pre>';

function urlResponse($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch); 
curl_close($ch);
return $response;

}
function processUrl($url,$apCode){ 
if(stripos($url,'?')>0)
       $url = $url.'&access_token='.$apCode;
else
       $url = $url.'?access_token='.$apCode;

return urlResponse($url);
}
?>

I guess the code below is wrong. I got the user data from facebook in JSON format but unfortunately I am not able to add user data in mysql using the PHP. How could we insert the json format data in mysql using php?

<?php
require('../conn.php');
$name = $url['id']['name'];
$first_name = $url['id']['first_name'];
$last_name = $url['id']['last_name'];
$hometown = $url['id']['hometown'];

{
$sql="insert into user values('','$name','$first_name','$last_name','$hometown')";
mysql_query($sql);
}

?>

<script type="text/javascript">window.location="../index.php"</script>

<html>
<head>
<title></title>
</head>
<body>
</body>
</html>

Here what you need to do , first the JSON returned data needs to be converted to array as

$response = json_decode($response,true);

Now with this data you have the array and you can use print_r($response) and see how the array looks like and use the data in the query.

Hope this helps

So, apparently I can't comment without 50 rep points - so whatever. I suspect your insert statement is off. Why do you have an empty string at the beginning? I hope that's not your primary key field. I would specify my fields if I were you and leave the auto-inc field out of it so it can auto increment :)

$sql="insert into user (`name`,`firstName`,`LastName`,`homeTown`) values('$name','$first_name','$last_name','$hometown')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM