gpt4 book ai didi

php - 如何使用PHP将网页获取到变量中

转载 作者:行者123 更新时间:2023-12-05 08:15:10 25 4
gpt4 key购买 nike

我想从网上下载一个页面,当你使用像 Firefox 这样的简单浏览器时,它是允许的,但是当我使用“file_get_contents”时,服务器拒绝并回复说它理解命令但不允许这样下载。

那怎么办?我想我在某些脚本(在 Perl 上)中看到了一种通过创建用户代理和 cookie 使您的脚本像真正的浏览器的方法,这使服务器认为您的脚本是真正的网络浏览器。

有没有人对此有想法,如何实现?

最佳答案

使用 CURL。

<?php
// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "example.com");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


// set the UA
curl_setopt($ch, CURLOPT_USERAGENT, 'My App (http://www.example.com/)');

// Alternatively, lie, and pretend to be a browser
// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);
?>

(来自http://uk.php.net/manual/en/curl.examples-basic.php)

关于php - 如何使用PHP将网页获取到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/692962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com