【问题标题】:Using Curl to get remote page source how to echo only one line of the code based on its number使用 Curl 获取远程页面源如何根据其编号仅回显一行代码
【发布时间】:2013-05-08 14:24:45
【问题描述】:

我正在工作 n 获取远程页面的源代码它从用户根据数组array('event_id', 'tv_id', 'tid', 'channel') 单击的 url 动态获取的远程页面的 url:我使用下面的代码来获取 who;e 页面源而且效果很好。

<?php
$keys = array('event_id', 'tv_id', 'tid', 'channel'); // order does matter
$newurl = 'http://lsh.streamhunter.eu/static/popups/';
foreach ($keys as $key)
    $newurl.= empty($_REQUEST[$key])?0:$_REQUEST[$key];

$newurl.='.html';
    function get_data($newurl) 
    { 
       $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,$newurl);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;

    }

    $html = get_data($newurl);

    echo $html

?>

这里的诀窍是我只想回显代码的第 59 行 怎么办?

【问题讨论】:

  • 你好像从来没有打电话 get_data
  • 第 59 行有一些 id 或类吗?如果是,你应该尝试 dom 解析

标签: php html regex curl


【解决方案1】:

在您的代码中,您使用的是 file_get_contents 而不是 get_data 函数,因此我将其从示例中删除。

<?php
    $keys = array('event_id', 'tv_id', 'tid', 'channel'); // order does matter
    $newurl = 'http://lsh.streamhunter.eu/static/popups/';
    foreach ($keys as $key)
        $newurl.= empty($_REQUEST[$key])?0:$_REQUEST[$key];

    $newurl.='.html';


    $html = file_get_contents($newurl);
    $html = explode("\n",$html);
    echo $html[58];

?>

【讨论】:

  • 如果我想要多行如何在echo $html[58];中分隔它们,经过一些调整后它现在可以工作了@
【解决方案2】:

您可能想使用 php 函数 file 将文件读入数组,然后使用它的索引。

$html = file($newurl);

echo $html[58];

【讨论】:

    【解决方案3】:

    如果您想准确获取从 $newurl 返回的数据的第 59 行,并且您不担心它会那么大,您可以使用 file($newurl)[58]

    如果你说的是来自curl 的数据,你可以使用explode("\n", $data) 得到一个类似的数组。

    【讨论】:

    • 我已将问题中的代码更新为 get_data 先生可以建议我的代码只显示第 59 行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    相关资源
    最近更新 更多