Home > Backend Development > PHP Tutorial > PHP implements curl or file_get_contents to obtain the page that requires authorization

PHP implements curl or file_get_contents to obtain the page that requires authorization

墨辰丷
Release: 2023-03-27 14:12:02
Original
1507 people have browsed it

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[&#39;content&#39;])? $_POST[&#39;content&#39;] : &#39;&#39;; 
header(&#39;content-type:application/json&#39;); 
echo json_encode(array(&#39;content&#39;=>$content)); 
?>
Copy after login

Use curl to get the server.php page

<?php 
$url = &#39;http://localhost/server.php&#39;; 
$param = array(&#39;content&#39;=>&#39;fdipzone blog&#39;); 
$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[&#39;http_code&#39;]==200){ 
 $data = json_decode($ret, true); 
 print_r($data); 
}else{ 
 echo &#39;POST Fail&#39;; 
} 
?>
Copy after login

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 = &#39;http://localhost/server.php&#39;; 
$param = array(&#39;content&#39;=>&#39;fdipzone blog&#39;); 

$opt = array( 
 &#39;http&#39; => array( 
  &#39;method&#39; => &#39;POST&#39;, 
  &#39;header&#39; => &#39;content-type:application/x-www-form-urlencoded&#39;, 
  &#39;content&#39; => 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 &#39;POST Fail&#39;; 
} 
?>
Copy after login

Use curl and file_get_contents to return The results are the same.

Array 
( 
 [content] => fdipzone blog 
)
Copy after login

For pages that require authorization, such as pages that use

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[&#39;PHP_AUTH_USER&#39;])) 
{ 
 header(&#39;WWW-Authenticate: Basic realm="localhost"&#39;); 
 header("HTTP/1.0 401 Unauthorized"); 
 exit; 
}else{ 
 if (($_SERVER[&#39;PHP_AUTH_USER&#39;]!= "fdipzone" || $_SERVER[&#39;PHP_AUTH_PW&#39;]!="654321")) { 
  header(&#39;WWW-Authenticate: Basic realm="localhost"&#39;); 
  header("HTTP/1.0 401 Unauthorized"); 
  exit; 
 } 
} 
$content = isset($_POST[&#39;content&#39;])? $_POST[&#39;content&#39;] : &#39;&#39;; 
header(&#39;content-type:application/json&#39;); 
echo json_encode(array(&#39;content&#39;=>$content)); 
?>
Copy after login

Set Defined account: fdipzone Password: 654321

In curl, there is a parameter which is

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 = &#39;http://localhost/server.php&#39;; 
$param = array(&#39;content&#39;=>&#39;fdipzone blog&#39;); 

$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, &#39;fdipzone:654321&#39;); // 加入这句 
$ret = curl_exec($ch); 
$retinfo = curl_getinfo($ch); 
curl_close($ch); 
if($retinfo[&#39;http_code&#39;]==200){ 
 $data = json_decode($ret, true); 
 print_r($data); 
}else{ 
 echo &#39;POST Fail&#39;; 
} 
?>
Copy after login

And file_get_contents If you want to send the account number and password, you need to manually splice the header

file_get_contents The requested program is modified to:

<?php 
$url = &#39;http://localhost/server.php&#39;; 
$param = array(&#39;content&#39;=>&#39;fdipzone blog&#39;); 

$auth = sprintf(&#39;Authorization: Basic %s&#39;, base64_encode(&#39;fdipzone:654321&#39;)); // 加入这句 

$opt = array( 
 &#39;http&#39; => array( 
  &#39;method&#39; => &#39;POST&#39;, 
  &#39;header&#39; => "content-type:application/x-www-form-urlencoded\r\n".$auth."\r\n", // 把$auth加入到header 
  &#39;content&#39; => 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 &#39;POST Fail&#39;; 
} 
?>
Copy after login

Related recommendations:

Detailed explanation of file_put_contents function in PHP

PHP uses file_get_contentsDetailed explanation of the steps to send an http request

##file_get_

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template