search
Homephp教程PHP源码PHP CURL 执行 Authorization 请求的例子

下面看一个利用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的内容即可。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools