Php 从远程服务器获取html页面内容的基本示例
代码如下:Php 从远程服务器获取html页面内容的基本示例,php,curl,Php,Curl,代码如下: /* * Example to fetch the example.com homepage into a file */ $curlObject = curl_init("http://AAA.BBB.CCC.DDD/");//AAA.BBB.CCC.DDD is the IP address of the remote server. $file = fopen("example_homepage.txt", "w"); curl_setopt($curlObject, C
/*
* Example to fetch the example.com homepage into a file
*/
$curlObject = curl_init("http://AAA.BBB.CCC.DDD/");//AAA.BBB.CCC.DDD is the IP address of the remote server.
$file = fopen("example_homepage.txt", "w");
curl_setopt($curlObject, CURLOPT_FILE, $file);
curl_setopt($curlObject, CURLOPT_HEADER, 0);
curl_exec($curlObject);
curl_close($curlObject);
fclose($file);
它是以这个例子为基础的。我正在学习卷发的基本用法。预期的输出是,在执行此php脚本后,应将安装在远程计算机(其IP为AAA.BBB.CCC.DDD
)中XAMPP服务器的htdocs
目录中的index.php
或index.html
的内容复制到示例_homepage.txt文件中
现在创建了example\u homepage.txt
文件,但该文件是空的。位于远程机器中安装的XAMPP服务器的htdocs
目录中的主页(index.php
或index.html
)的内容不会复制到新创建的example\u homepage.txt
中
问题是为什么以及如何解决这个问题?
curl\u setopt($curlObject,CURLOPT\u RETURNTRANSFER,true)设置选项CURLOPT\u RETURNTRANSFER否则你什么也得不到
<?php
$curlObject = curl_init("http://example.com/");
curl_setopt($curlObject, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curlObject);
curl_close($curlObject);
file_put_contents('example_homepage.txt', $result);
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.de/">here</A>.
</BODY></HTML>