Home > Backend Development > PHP Tutorial > How does PHP Curl handle 301 redirects on web pages?

How does PHP Curl handle 301 redirects on web pages?

WBOY
Release: 2024-03-08 16:38:01
Original
737 people have browsed it

PHP Curl如何处理网页的301跳转?

Title: How does PHP Curl handle 301 jumps on web pages?

In the process of web crawling or data scraping, we often encounter situations where web pages return 301 redirects when requested. At this time, we need to use PHP Curl to handle this situation to ensure that the content of the target web page can be correctly obtained. In the following example, I will demonstrate how to use PHP Curl to handle 301 redirects to web pages. Let’s take a look.

<?php

// 目标网页的URL
$url = 'https://example.com';

// 初始化Curl
$ch = curl_init();

// 设置Curl选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 开启自动跳转

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

// 检查是否发生301跳转
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 301) {
    // 获取重定向后的URL
    $redirectUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
    
    // 重新请求重定向后的URL
    curl_setopt($ch, CURLOPT_URL, $redirectUrl);
    $response = curl_exec($ch);
}

// 关闭Curl
curl_close($ch);

// 输出获取到的页面内容
echo $response;

?>
Copy after login

In the above example, we first define the URL of the target web page and initialize a Curl session. Then set some Curl options, including setting the option to track 301 redirects to true. Then execute a Curl request to obtain the web page content.

We use the curl_getinfo function to check whether the requested HTTP status code is 301. If it is a 301 redirect, we will get the redirected URL and use Curl again to request the redirected URL to obtain Final content. Finally, close the Curl session and output the obtained page content.

Through the above example, we can successfully handle the 301 jump of the web page and ensure that the content of the target web page can be correctly obtained when using PHP Curl. hope it helps you!

The above is the detailed content of How does PHP Curl handle 301 redirects on web pages?. 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