使用Curl获取远程页面源代码如何根据其编号只回显一行代码



我正在工作获取远程页面的源代码,该远程页面的url从用户根据数组array('event_id', 'tv_id', 'tid', 'channel')点击的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行怎么做呢?

在您的代码中,您使用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];
?>

您可能希望使用php函数file将文件读取到数组中,然后使用其索引。

$html = file($newurl);
echo $html[58];

如果您想获得$newurl返回的第59行数据,并且您不担心它太大,您可以使用file($newurl)[58]

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

最新更新