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


Using CURL to get remote page source code and create new HTML file with variable name

在做了3个小时的研究和阅读之后,我决定发布这个问题来分享我所达到的并寻求你的帮助,

这是我想做的,我已经创建了。php文件执行3个操作:

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

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

在当前窗口打开该文件

我试图使它基本在应用它在google.com作为远程页面和本地主机。

显示在localhost/test.php中的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将是从上面的代码中获得的数字

如何获取xxxxxxxxxxxx. HTML的代码并在mysite.com/static/popups/上创建名为xxxxxxxxxxxx. HTML的HTML文件

<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获取url的查询字符串。
然后,使用parse_str解析它。
在此之后,只需在您要访问的url中填写一些零碎的内容。

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

如果没有定义参数,则返回0