Hongmu Notes
Home Language Notes PHP forges request headers to collect web pages
Language Notes PHP PHP collection

PHP forges request headers to collect web pages

PHP forges request headers to collect web pages

In PHP, the cURL library can be used to simulate the sending of HTTP requests and configure request headers, thereby forging headers for web page scraping. Here is an example code:

// 创建 cURL 句柄
$ch = curl_init();

// 设置请求 URL
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com');

// 设置请求头信息
$headers = array(
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language: en-US,en;q=0.5',
    'Referer: https://www.google.com/',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// 设置其他 cURL 选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// 执行 HTTP 请求
$response = curl_exec($ch);

// 检查是否有错误发生
if(curl_errno($ch)) {
    echo 'cURL error: ' . curl_error($ch);
}

// 关闭 cURL 句柄
curl_close($ch);

// 输出响应结果
echo $response;

In the above code, we first create a cURL handle and set the requested URL as well as the request headers. The request headers include common fields such as User-Agent, Accept, Accept-Language, and Referer; you can add or modify these fields as needed.

Additionally, we have configured other cURL options—such as CURLOPT_RETURNTRANSFER, CURLOPT_FOLLOWLOCATION, CURLOPT_SSL_VERIFYHOST, and CURLOPT_SSL_VERIFYPEER—which allow you to control cURL's behavior, for example, whether to return the response, whether to follow redirects, or whether to verify SSL certificates, among other things.

Finally, we send an HTTP request and display the response on the page. If an error occurs, we also output the cURL error message using the `curl_errno` and `curl_error` functions.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP capture website source code and json parsing

PHP capture website source code and json parsing Language Notes PHP PHP collection

Recently, I wanted to collect the names of some resource websites, so I found a fun one when I collected it. The opposite website was loaded by ajax technology, so I found the api, so I just wrote the code directly to put it in the warehouse. The code is backed up here and will be used in the future. & lt; ? php $date = file_get_contents(' Website address ' ); $date = json_deco…
👁 136
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 180

Recommended reading

Fruit and Vegetable Retail Store Website Template 1182

Fruit and Vegetable Retail Store Website Template 1182 Practical Collection Yiyou template

This EyouCMS template is ideal for fruit and vegetable retail stores, featuring a fresh and natural design style that effectively showcases fresh produce, store branding, and promotional content. It helps fresh produce stores attract local customers online and enhance their brand awareness. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Eyou...
👁 57
Responsive Women's Fashion Retail Display Website Template 0326

Responsive Women's Fashion Retail Display Website Template 0326 Practical Collection Yiyou template

An Eyou CMS responsive website template designed for the fashion women's wear display industry. Its stylish and premium design is ideal for showcasing women's wear products, brand image, new product launches, and franchise opportunities. This template helps women's wear brands attract consumers and distributors online. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for Eyou CMS | Eyou CMS (Ey...
👁 48
(PC + WAP) Adult Education Examination Self-Study Page Landing Page – PBootCMS Website Template; Download Source Code for Adult Education and Training Single-Page Websites – 0669

(PC + WAP) Adult Education Examination Self-Study Page Landing Page – PBootCMS Website Template; Download Source Code for Adult Education and Training Single-Page Websites – 0669 Practical Collection pbootcms Template

A PbootCMS landing page template designed for adult education examinations and self-study training programs, compatible with both PC and mobile devices. The design focuses on driving marketing conversions, featuring a compact page structure that highlights course advantages, faculty expertise, and the enrollment portal. Ideal for educational institutions using Baidu PPC advertising or SEM campaigns to effectively increase the enrollment conversion rate for adult education courses. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password:...
👁 35
The magic tool for preventing lags in web writing (how to avoid lags in web writing)

The magic tool for preventing lags in web writing (how to avoid lags in web writing) Hobbies Memo notes

A universal template for web writing, an artifact that won’t get stuck. Advance the plot from the protagonist's perspective. Enter the event and the protagonist makes an action! Others were shocked at the reversal of actions, the unexpectedness, the possibility of increasing the number, and the ending without the protagonist being pretentious (focusing on the core to be cool and pretentious). Everyone was shocked and speculated on the protagonist's strength. The protagonist harvests, takes stock, and pulls targets! Render the situation, expand (the situation causes sudden events) the protagonist's thinking, pull out the target and enter the main perspective of the next protagonist, and start...
👁 139
pbootCMS Website Builder: Beginner's Tutorial and Practical Source Code

pbootCMS Website Builder: Beginner's Tutorial and Practical Source Code Practical Collection pb tool

This video was recorded spontaneously, so there might be some parts where the explanation isn't perfect – we'd really appreciate your understanding! Download the source code for this video tutorial 🔒 Hidden content: Please leave a comment to access the link: https://pan.baidu.com/s/15kHHZY68jIv-Eun9g2UJfg?pwd=aw05 Extraction code: aw05 – Simply copy this link and open the Baidu Netdisk mobile app to access the content – it'll make things much easier!
👁 262
Responsive Biotechnology Food Website Template 1183

Responsive Biotechnology Food Website Template 1183 Practical Collection Yiyou template

An eyouCMS responsive website template designed for the biotechnology and food industries. Its modern, tech-driven and health-focused design is ideal for showcasing biotechnology products, health foods, and R&D capabilities. This template helps biotechnology companies effectively present their brands online and attract both partners and consumers. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YououCMS | Youou...
👁 30