This article mainly introduces how to implement curl or file_get_contents in PHP to obtain the page that requires authorization. Interested friends can refer to it. I hope it will be helpful to everyone.
For example, the page to be obtained: http://localhost/server.php
##
<?php $content = isset($_POST['content'])? $_POST['content'] : ''; header('content-type:application/json'); echo json_encode(array('content'=>$content)); ?>
Use curl to get the server.php page
<?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 the php curl extension installed, use file_get_contentsYou can also initiate a request and 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'; } ?>
Array ( [content] => fdipzone blog )
htpasswd .htaccess to set directory access permissions, Directly using the above method will return the 401 Unauthorized error.
This example does not use htpasswd .htaccess to control access permissions, but uses$_SERVER['PHP_AUTH_USER'] and $ _SERVER['PHP_AUTH_PW']These two server parameters.
http://localhost/server.php Change 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)); ?>
CURLOPT_USERPWD. We can use this parameter to send the account password when requesting.
curl_setopt($ch, CURLOPT_USERPWD, 'Account: Password');
The program requested by curl 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'); // 加入这句 $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'; } ?>
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')); // 加入这句 $opt = array( 'http' => array( 'method' => 'POST', 'header' => "content-type:application/x-www-form-urlencoded\r\n".$auth."\r\n", // 把$auth加入到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'; } ?>
Detailed explanation of file_put_contents function in PHP
PHP uses file_get_contentsDetailed explanation of the steps to send an http request
contentsDetailed explanation of function introduction and usage
The above is the detailed content of PHP implements curl or file_get_contents to obtain the page that requires authorization. For more information, please follow other related articles on the PHP Chinese website!