PHP implements Curl or file_get_contents to obtain a method that requires an authorization page

Source: Internet
Author: User
This article mainly introduces the PHP implementation of curl or file_get_contents to obtain the required authorization page method, interested in the friend's reference, I hope to be helpful to everyone.

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

<?php $content = isset ($_post[' content ')? $_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, use the file_get_contents You can also implement an originating request to get the page return 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 (  [content] = 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, while using $_server[' Php_auth_user ' and $_server[' PHP_AUTH_PW '] These two server parameters.

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

<?php if (!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 '); echo json_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.

curl_setopt($ch, CURLOPT_USERPWD, '帐号:密码');

The program that Curl requests is modified to:

<?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 $opt = Array (' http ' = = ' + '   , ' method ' = ' POST ', ' header ' = ' + '   content-type:application/ X-www-form-urlencoded\r\n ". $auth." \ r \ n ",//Add $auth to 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 ';}?>

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.