使用CURL获取远程页面源代码并创建具有变量名称的新HTML文件

时间:2013-05-03 00:00:18

标签: php curl

经过3个小时的研究和阅读后,我决定发布这个问题,分享我到达的内容并寻求你的帮助,

这是我想要做的事情我已经创建了.php文件执行3次操作:

1-curl函数获取远程页面的源代码。

2 - 创建一个新的html文件,其中包含从远程页面获取的代码。

在当前窗口中打开此文件

我首先尝试将其作为远程页面和本地主机在google.com上应用。

文件test.php,它存在于localhost / test.php中,代码为:

<?php
    //Get the url
    $url = "http://google.com";

    //Get the html of url
    function get_data($url) 
    { 
       $ch = curl_init();
       $timeout = 5;
       //$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.X.Y.Z Safari/525.13.";
       $userAgent = "IE 7 – Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)";
      curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
      curl_setopt($ch, CURLOPT_FAILONERROR, true);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_AUTOREFERER, true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;

    }

    $html = file_get_contents($url);
    $fp = @fopen('google.html', 'w') or die('Could not open file, or fike does not exist and failed to create.');
    $mytext = $html;
    @fwrite($fp, $mytext) or die('Could not write to file.');
    ?>
    <script type="text/javascript">
    window.location.href = 'google.html'; //Will take you to Google.
    </script>

它完美无缺:D所以我已经开始在以下代码中动态获取远程页面链接的实际站点上应用:

<html>
<script type="text/javascript">
function getQueryVariable(variable,def) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return def;
}
function redirect(){
    window.location.href = 'static/popups/'+getQueryVariable('event_id',0)+getQueryVariable('tv_id',0)+getQueryVariable('tid',0)+getQueryVariable('channel',0)+'.html';
} 
</script>
<body onload="redirect()">
<style>body{background-color: #000000; text-align: center;}</style>
</body></html> 

所以生成的链接将是这样的http://remotepage.com/static/popups/xxxxxxxxxxxxx.html,其中xxxxxxxxxxxxx将是从上面的代码中获得的数字

如何获取xxxxxxxxxxxxx.html的代码并在mysite.com/static/popups /

创建名为xxxxxxxxxxxxx.html的html文件

1 个答案:

答案 0 :(得分:0)

<script type="text/javascript">
function getQueryVariable(variable,def) {
  var query = window.location.search.substring(1);  //Returns the query string of the URL.   Eg, anything after "?" in the url.
  var vars = query.split("&");                      //This and the next couple lines just find the right key in the url
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return def;
}
function redirect(){
    window.location.href = 'static/popups/'+getQueryVariable('event_id',0)+getQueryVariable('tv_id',0)+getQueryVariable('tid',0)+getQueryVariable('channel',0)+'.html';
} 
</script>

因此,快速简便的方法是使用parse_url获取网址的查询字符串。
然后,使用parse_str解析该问题 在那之后,只需填写您想要去的网址。

Parse_str返回一个命名数组。因此,如果您的查询字符串为?event_id=2&tv_id=100,则可以在解析字符串后转到$arr['event_id']来获取event_id。 然后,用匹配变量替换每个函数调用 getQueryVariable('event_id',0)将替换为$ arr ['event_id']。

如果未定义参数,则会得到0。

相关问题