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

May 22, 2018 pm 04:30 PM
curl file

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

##

1

2

3

4

5

<?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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<?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.

1

2

3

4

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<?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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<?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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
How to realize the mutual conversion between CURL and python requests in python How to realize the mutual conversion between CURL and python requests in python May 03, 2023 pm 12:49 PM

Both curl and Pythonrequests are powerful tools for sending HTTP requests. While curl is a command-line tool that allows you to send requests directly from the terminal, Python's requests library provides a more programmatic way to send requests from Python code. The basic syntax for converting curl to Pythonrequestscurl command is as follows: curl[OPTIONS]URL When converting curl command to Python request, we need to convert the options and URL into Python code. Here is an example curlPOST command: curl-XPOST https://example.com/api

How to convert php blob to file How to convert php blob to file Mar 16, 2023 am 10:47 AM

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

PHP8.1 released: Introducing curl for concurrent processing of multiple requests PHP8.1 released: Introducing curl for concurrent processing of multiple requests Jul 08, 2023 pm 09:13 PM

PHP8.1 released: Introducing curl for concurrent processing of multiple requests. Recently, PHP officially released the latest version of PHP8.1, which introduced an important feature: curl for concurrent processing of multiple requests. This new feature provides developers with a more efficient and flexible way to handle multiple HTTP requests, greatly improving performance and user experience. In previous versions, handling multiple requests often required creating multiple curl resources and using loops to send and receive data respectively. Although this method can achieve the purpose

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Tutorial on updating curl version under Linux! Tutorial on updating curl version under Linux! Mar 07, 2024 am 08:30 AM

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

Rename files using java's File.renameTo() function Rename files using java's File.renameTo() function Jul 25, 2023 pm 03:45 PM

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

See all articles