如何使用fwrite()将curl中的html插入到新创建的页面中
我正在使用看似标准的 fwrite 代码将 html 插入新页面。该代码从 html 表单 (url) 获取输入,使用代理获取源代码并创建一个标题为时间戳的新页面。所有这些都有效,无效的是将 html 源代码写入新页面。
下面的代码输出一个时间戳并创建一个新页面,但是该页面是空的,而其中应该有代码。任何指示或建议将不胜感激。
<? $url = $_POST['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '123.30.185.86:3128');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_exec ($ch); $curl_scraped_page = curl_exec($ch);
$FileName = time();
$FileHandle = fopen($FileName, 'w') or die("can't open
file");
fwrite($FileHandle, $curl_scraped_page);
curl_close($ch);
echo $FileName; ?>
I am using what appears to be standard fwrite code to insert html into a new page. The code takes input from a html form (url) grabs the source code using a proxy and creates a new page titled with a timestamp. All that works, what is not working is writing the html source code into the new page.
The bellow outputs a timestamp and creates a new page, however the page is empty whereas it should have code in it. Any pointers or suggestions would be much appreciated.
<? $url = $_POST['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '123.30.185.86:3128');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_exec ($ch); $curl_scraped_page = curl_exec($ch);
$FileName = time();
$FileHandle = fopen($FileName, 'w') or die("can't open
file");
fwrite($FileHandle, $curl_scraped_page);
curl_close($ch);
echo $FileName; ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是公正的
并输出你获取的内容......
should be just
and to output what you fetched...
您可以使用 file_put_contents 代替。
该函数与依次调用 fopen()、fwrite() 和 fclose() 将数据写入文件相同。
You may use file_put_contents instead.
This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.