使用PHP CURL获取HTML文件的HTML代码

时间:2013-02-01 08:43:52

标签: php curl

我正在研究localserver 192.168.1.1的任务。我想获取页面的数据

  

192.168.1.1/userRpm/SystemStatisticRpm.htm?Num_per_page=100

我想显示此文件返回的HTML代码。

当我在终端上运行curl命令时 curl -u admin:admin PageURL (返回页面代码)

但是当我使用PHP curl时,它会将我重定向到192.168.1.1

有人能帮助我吗?

我的代码是:

<?php
$json_url="http://192.168.1.1/userRpm/SystemStatisticRpm.htm?Num_per_page=100";
$username="admin";
$password="admin";
$ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $json_url);
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type:application/json')); 
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

  $result=curl_exec($ch);
  curl_close ($ch);

2 个答案:

答案 0 :(得分:2)

请检查

$ch= curl_init();
curl_setopt ($ch1, CURLOPT_URL, 'http://192.168.1.1/userRpm/SystemStatisticRpm.htm?Num_per_page=100' );
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1,CURLOPT_VERBOSE,1);
curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
curl_setopt ($ch1, CURLOPT_REFERER,'http://www.google.com');  //just a fake referer
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1,CURLOPT_POST,0);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 20);

$htmlContent= curl_exec($ch1);

答案 1 :(得分:1)

试试这个:

$ch = curl_init("addrress");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);