Example of php curl Authorization request execution

Source: Internet
Author: User
Tags curl http request

Php curl extension can help us quickly implement HTTP requests.

When using the doubanoauth login interface, we need to send such an http request:

The code is as follows: Copy code

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

Run the following command in the command line:

The code is as follows: Copy code

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

Php curl sends Authorization HTTP request

Http curl:

 

The code is as follows: Copy code
$ 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 );

If you need http curl to implement GET requests, see the following:

Http curl:

 

The code is as follows: Copy code
$ 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/'.20.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 );

Then it processes the received results. Generally, the interface class will return JSON or XML, and the collection class will directly filter the HTML content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.