duanniu3385 2012-10-04 14:56
浏览 58

从PHP中的cURL请求中获取Foursquare access_token

I'm testing a cURL function to get back the access token for a given user on Foursquare. The following function prints to the screen the access token in a json format

{"access_token":"5B25GW5LF3L4W04PALHK32X5Z3YGNUUVDHM2TFBQOWZSQ121"} 

I'm having problems in extracting the value of the access token.

This is the function I'm using:

function get_access_token($code){

        $url = "https://foursquare.com/oauth2/access_token";

        $fields = array(
            'client_id' => urlencode($this->client_id),
            'client_secret' => urlencode($this->client_secret),
            'grant_type' => 'authorization_code',
            'redirect_uri' => urlencode($this->redirect_url),
            'code' => $code
        );

        //url-ify the data for the POST
        foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
        rtrim($fields_string, '&');

        //open connection
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

        //execute post
        $result = curl_exec($ch);
        //close connection
        curl_close($ch);

        return var_dump($result) /* prints bool(true) */;
    }

How to I store this access token in a variable?

Thank you

  • 写回答

2条回答 默认 最新

  • dqybeh2884 2012-10-09 20:49
    关注

    By default curl_exec does not return the response, but a value indicating success or failure. Visit http://php.net/manual/en/function.curl-exec.php for more information.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程