Curl or File_get_contents Gets the method that requires the authorization page

Source: Internet
Author: User

Original: http://blog.csdn.net/fdipzone/article/details/44475801

The original view is clearer because the blog's editor is rotten. Go back to backup.

Today because of work needs, need to use curl/file_get_contents to obtain authorization (Authorization) of the page content, resolved after writing this article to share to everyone

PHP Curl extension, the ability to initiate Post/get requests on the server side, access to the page, and the ability to retrieve the page return data.

For example, to get the page:http://localhost/server.php

<? PHP $content isset ($_post$_post[' content ']: '; Header (' Content-type:application/json '); echo json_encode (array(' content ' =$content));? >

Get server.php Page Using Curl

<?PHP$url= ' http://localhost/server.php ';$param=Array(' content ' = ' Fdipzone blog ');$ch=curl_init (); curl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_post,true); curl_setopt ($ch, Curlopt_postfields,Http_build_query($param)); Curl_setopt ($ch, Curlopt_returntransfer,true);$ret= Curl_exec ($ch);$retinfo= Curl_getinfo ($ch); Curl_close ($ch);if($retinfo[' Http_code ']==200){    $data= Json_decode ($ret,true); Print_r($data);}Else{    Echo' POST Fail ';}?>

If the service does not have a php curl extension installed, using file_get_contents can also implement a request to get the page back data

<?PHP$url= ' http://localhost/server.php ';$param=Array(' content ' = ' Fdipzone blog ');$opt=Array(    ' http ' =Array(        ' Method ' = ' POST ', ' header ' = ' content-type:application/x-www-form-urlencoded ', ' content ' =Http_build_query($param)    ));$context=stream_context_create($opt);$ret=file_get_contents($url,false,$context);if($ret){    $data= Json_decode ($ret,true); Print_r($data);}Else{    Echo' POST Fail ';}?>

The results returned using curl and file_get_contents are the same.

Array (    = = Fdipzone blog)

For pages that require authorization, such as pages that use Htpasswd+.htaccess to set Directory Access, the 401 unauthorized error is returned directly with the method above .

This example does not use htpasswd+.htaccess to control access, but uses the two server parameters $_server[' Php_auth_user ' and $_server[' PHP_AUTH_PW '].

To learn about Htpasswd+.htaccess's friends, you can visit the article I wrote earlier, "Generate encrypted password files using Apache htpasswd, and use. htaccess to control Directory Access"

http://localhost/server.php modified to:

<?PHPif(!isset($_server[' Php_auth_user '])) {     Header(' Www-authenticate:basic realm= ' localhost '); Header("Http/1.0 401 Unauthorized"); Exit; }Else{     if(($_server[' Php_auth_user ']!= "Fdipzone" | |$_server[' PHP_AUTH_PW ']!= "654321")) {        Header(' Www-authenticate:basic realm= ' localhost '); Header("Http/1.0 401 Unauthorized"); Exit; }}$content=isset($_post[' content ']?$_post[' content ']: ';Header(' Content-type:application/json ');EchoJson_encode (Array(' content ' =$content));?>

Set account number:fdipzone password:654321

In Curl, one of the parameters is curlopt_userpwd, and we can use this parameter to send the password of the account in the past upon request.

<?PHP$url= ' http://localhost/server.php ';$param=Array(' content ' = ' Fdipzone blog ');$ch=curl_init (); curl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_post,true); curl_setopt ($ch, Curlopt_postfields,Http_build_query($param)); Curl_setopt ($ch, Curlopt_returntransfer,true); curl_setopt ($ch, Curlopt_userpwd, ' fdipzone:654321 ');//Add this sentence$ret= Curl_exec ($ch);$retinfo= Curl_getinfo ($ch); Curl_close ($ch);if($retinfo[' Http_code ']==200){    $data= Json_decode ($ret,true); Print_r($data);}Else{    Echo' POST Fail ';}?>

and file_get_contents if you want to send the account and password, you need to manually splice header
File_get_contents The requested program is modified to:

<?PHP$url= ' http://localhost/server.php ';$param=Array(' content ' = ' Fdipzone blog ');$auth=sprintf(' Authorization:basic%s ',Base64_encode(' fdipzone:654321 '));//Add this sentence$opt=Array(    ' http ' =Array(        ' Method ' = ' POST ', ' header ' = ' content-type:application/x-www-form-urlencoded\r\n '.$auth." \ r \ n ",//Add the $auth to the header' Content ' =Http_build_query($param)    ));$context=stream_context_create($opt);$ret=file_get_contents($url,false,$context);if($ret){    $data= Json_decode ($ret,true); Print_r($data);}Else{    Echo' POST Fail ';}?>

Curl or file_get_contents get a way to authorize pages

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.