Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Curl和JSON混淆_Php_Api_Curl_Json - Fatal编程技术网

PHP Curl和JSON混淆

PHP Curl和JSON混淆,php,api,curl,json,Php,Api,Curl,Json,我试图了解如何获得访问令牌与CURL一起工作 $url = "https://api.com/oauth/access_token"; $access_token_parameters = array( 'client_id' => '', 'client_secret' => '', 'grant_type' => '', 'redirect_uri' => '',

我试图了解如何获得访问令牌与CURL一起工作

$url = "https://api.com/oauth/access_token";
$access_token_parameters = array(
    'client_id'     =>     '',
    'client_secret'     =>     '',
    'grant_type'        =>     '',
    'redirect_uri'      =>     '',
    'code'          =>     $_GET['code']
);
$curl = curl_init($url);
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$access_token_parameters);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
我应该得到一个JSON字符串,对吗

我尝试了各种方法来输出字符串

$test = json_decode($result);
print_r($test); 

$arr = json_encode($result,true);
foreach($arr as $val){
    echo $val['access_token'];  
}
我做错了吗

我认为正确的JSON输出应该是这样的:

{
    "access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
    "user": {
        "id": "1574083",
        "username": "snoopdogg",
        "full_name": "Snoop Dogg",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg"
    }
}
但这不起作用?我正在尝试从服务器获取访问令牌


任何帮助都将不胜感激!谢谢

尝试使用下面的功能获取内容

 $response = get_web_page($url);
 $resArr = array();
 $resArr = json_decode($response);
//echo"<pre>"; print_r($resArr); echo"</pre>";
    function get_web_page($url) {
          $options = array (CURLOPT_RETURNTRANSFER => true, // return web page
        CURLOPT_HEADER => false, // don't return headers
        CURLOPT_FOLLOWLOCATION => true, // follow redirects
        CURLOPT_ENCODING => "", // handle compressed
        CURLOPT_USERAGENT => "test", // who am i
        CURLOPT_AUTOREFERER => true, // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
        CURLOPT_TIMEOUT => 120, // timeout on response
        CURLOPT_MAXREDIRS => 10 ); // stop after 10 redirects
          $ch = curl_init ( $url );
          curl_setopt_array ( $ch, $options );
          $content = curl_exec ( $ch );
          $err = curl_errno ( $ch );
          $errmsg = curl_error ( $ch );
          $header = curl_getinfo ( $ch );
          $httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
          curl_close ( $ch );
          $header ['errno'] = $err;
          $header ['errmsg'] = $errmsg;
          $header ['content'] = $content;
          return $header ['content'];
         }
$response=get\u web\u页面($url);
$resArr=array();
$resArr=json_decode($response);
//回声“;印刷费($resArr);回声“;
函数获取网页($url){
$options=array(CURLOPT_RETURNTRANSFER=>true,//返回网页
CURLOPT_HEADER=>false,//不返回头
CURLOPT_FOLLOWLOCATION=>true,//跟随重定向
CURLOPT_ENCODING=>“”,//句柄已压缩
CURLOPT_USERAGENT=>“test”//我是谁
CURLOPT_AUTOREFERER=>true,//在重定向时设置referer
CURLOPT_CONNECTTIMEOUT=>120,//连接超时
CURLOPT_TIMEOUT=>120,//响应超时
CURLOPT_MAXREDIRS=>10);//在10次重定向后停止
$ch=curl\u init($url);
curl_setopt_数组($ch$options);
$content=curl\u exec($ch);
$err=curl\u errno($ch);
$errmsg=curl\u error($ch);
$header=curl\u getinfo($ch);
$httpCode=curl\u getinfo($ch,CURLINFO\u HTTP\u代码);
卷曲关闭($ch);
$header['errno']=$err;
$header['errmsg']=$errmsg;
$header['content']=$content;
返回$header['content'];
}

尝试示例时会发生什么?你会犯什么样的错误?另外,不要打印或迭代结果。只要试试
var\u dump