小编Vít*_*peã的帖子

php使用curl获取html并将其保存在txt上

我一直在学习用PHP编程,我正在尝试使用CURL编写PHP脚本而不是file_get_contents($ url)

我想要了解更多关于Curl的原因是因为它更快,更灵活但更难

所以,我编码这个从一些URL下载HTML代码并将其保存在TXT上

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.google.pt");
curl_setopt($ch, CURLOPT_HEADER, 0);

//should save html in $html
$html = curl_exec($ch);

//save html on txt
$fh = fopen('html.txt', 'w') or die("can't open file");
        fwrite($fh, $html);
        fclose($fh);

curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

但相反,curl_exec(); 返回1并在我的页面上打印代码

无论如何要做我想要的?

php curl

2
推荐指数
1
解决办法
2338
查看次数

下载任何网站的HTML并在PHP中回显它

编辑这篇文章,我认为我的问题被解释错了:

我希望我的php页面可以从任何页面下载HTML代码,我实际上可以期待一个," http://www.lolnexus.com/ "并且我制作了这段代码

$nexus= file_get_contents('http://www.lolnexus.com/');

$myFile = "nexus.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $nexus);
fclose($fh);
Run Code Online (Sandbox Code Playgroud)

结果是:这个

<html><head><title>Object moved</title></head><body>
Run Code Online (Sandbox Code Playgroud)

对象移到这里.


如果我将我的网址更改为其他网页,它工作正常.....

谢谢阅读

html php

0
推荐指数
1
解决办法
343
查看次数

标签 统计

php ×2

curl ×1

html ×1