Home > php教程 > PHP源码 > body text

PHP CURL 执行 Authorization 请求的例子

WBOY
Release: 2016-06-08 17:22:29
Original
1748 people have browsed it

下面看一个利用PHP CURL 实现HTTP请求豆瓣OAuth登录接口时的一个例子,希望此例子对各位同学会带来帮助哦

<script>ec(2);</script>

PHP CURL 扩展可以帮助我们快速实现HTTP请求。

php curl http

在使用豆瓣OAuth登录接口时,我们需要发送这样的HTTP REQUEST 请求:

 代码如下 复制代码

    GET /v2/user/~me HTTP/1.1
    Host: https://api.douban.com
    Authorization: Bearer a14afef0f66fcffce3e0fcd2e34f6ff4

在命令行中我们这样执行:

 代码如下 复制代码

    curl "https://api.douban.com/v2/user/~me"
         -H "Authorization: Bearer a14afef0f66fcffce3e0fcd2e34f6ff4"

PHP CURL 发送 Authorization HTTP 请求

HTTP CURL 执行 POST  方法:

 

 代码如下 复制代码
   $crl = curl_init();
    $headr = array();
    $headr[] = 'Authorization: '.$douban_user_name.' '.$accesstoken;
    curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
    curl_setopt($crl, CURLOPT_POST,true);
    $rest = curl_exec($crl);
    curl_close($crl);
    print_r($rest);

如果需要HTTP CURL实现GET请求,请看下面:

HTTP CURL 执行 GET  方法:

 

 代码如下 复制代码
   $header = array();
    $header[] = 'Authorization: '.$data->douban_user_name.' '.$data->access_token;
    $ch = curl_init(); www.111cn.net
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_URL, 'https://api.douban.com/v2/user/'.$data->douban_user_id);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    print_r($response);

然后就是处理接收到的结果,一般接口类会返回JSON或者XML,采集类就直接过滤HTML的内容即可。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!