Php 我能够以json格式从facebook获取数据,但无法插入数据库。

Php 我能够以json格式从facebook获取数据,但无法插入数据库。,php,mysql,json,facebook,Php,Mysql,Json,Facebook,代码运行良好,但我无法在mysql数据库中插入用户数据 <?php $facebookAppAuthUrl = 'https://graph.facebook.com/oauth/access_token'; $facebookGraphUrl = 'https://graph.facebook.com'; $facebookClientId = ''; // Put your App Id here. $facebookRedirectUrl = ''; // Redirect url

代码运行良好,但我无法在mysql数据库中插入用户数据

<?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);
}
?>

我猜下面的代码是错误的。我从facebook获得了JSON格式的用户数据,但不幸的是,我无法使用PHP在mysql中添加用户数据。如何使用php在mysql中插入json格式的数据

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

这里您需要做的是,首先需要将JSON返回的数据转换为数组作为

现在有了这些数据,就有了数组,可以使用print\r($response)查看数组的外观,并在查询中使用数据


希望这能有所帮助,所以,显然我不能在没有50个代表点的情况下发表评论,所以不管怎样。我怀疑你的插入语句是错的。为什么开头有一个空字符串?我希望这不是你的主要领域。如果我是您,我会指定我的字段,并将auto inc字段保留在其中,以便它可以自动递增:)


您收到错误消息了吗?我看到您有json_解码(processUrl($url,$ApCode));使用json_解码(processUrl($url,$ApCode),true);并检查数组,然后可以在插入查询中使用
$response = json_decode($response,true);
$sql="insert into user (`name`,`firstName`,`LastName`,`homeTown`) values('$name','$first_name','$last_name','$hometown')";