doumeng3188 2013-02-01 08:43
浏览 224
已采纳

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

I am working on a task of localserver 192.168.1.1. I want to fetch the data of a page

192.168.1.1/userRpm/SystemStatisticRpm.htm?Num_per_page=100

I want to display the HTML code returned by this file.

When I am running curl command on terminal like curl -u admin:admin PageURL ( Its returning the code of page)

But when I am using PHP curl then its redirecting me to 192.168.1.1

Can anybody help me ?

My code is :

<?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条回答 默认 最新

  • drcomwc134525 2013-02-01 09:09
    关注

    Please check this

    $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条)

报告相同问题?

悬赏问题

  • ¥60 Matlab联合CRUISE仿真编译dll文件报错
  • ¥15 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId