【问题标题】:make curl get the final url and use it for another function让 curl 获取最终的 url 并将其用于另一个功能
【发布时间】:2013-05-05 02:19:14
【问题描述】:

我的 URL 包含以下示例中的变量:http://remotesite.com/index1.php?option=com_lsh&view=lsh&event_id=149274&tid=372536&channel=0

此 URL 重定向到 http://remotesite.com/static/popups/xxxxxxxxxxx.html

xxxxxxxxxxx 是来自第一个链接的变量,event_id=149274, tid=372536, channel=0

然后 xxxxxxxxxxx = 1492743725360 链接将是:

http://remotesite.com/static/popups/1492743725360.html

如何从链接 http://remotesite.com/index1.php?option=com_lsh&view=lsh&event_id=149274&tid=372536&channel=0 在 php 中自动获取此链接

那我就用curl函数获取1492743725360.html的源码

使用:

<?php
    //Get the url
    $url = "http:// remotesite.com/static/popups/1492743725360.html";

    //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);
    echo $html;
?>

【问题讨论】:

  • 你能发布一个html响应的例子吗?
  • html 响应什么?
  • $html,如果我没记错的话,您是在尝试抓取链接吗?
  • 我有点困惑。您展示了与 cURL 相关的 get_data() 函数,但实际上使用的是 file_get_contents();

标签: php curl


【解决方案1】:
$url = 'http://remotesite.com/index1.php?option=com_lsh&view=lsh&event_id=149274&tid=372536&channel=0';
$parsed = parse_url( $url );
parse_str( $parsed['query'], $data );
echo $newurl = 'http://remotesite.com/static/popups/'.
               $data['event_id'].$data['tid'].$data['channel'].
               '.html';

输出:

http://remotesite.com/static/popups/1492743725360.html

【讨论】:

  • @andery 如果我想在新函数中使用 $newurl 作为获取页面内容 curl 函数,我会在新函数中写 $url = $newurl 对吗??
  • 只需调用 $html = file_get_contents($newurl);或调用 $html = get_data($newurl);
【解决方案2】:

使用名为 parse_url() 的函数;第一个参数是整个 URL。这将返回一个解析了 URL 的数组,您需要的部分是位于数组键“查询”上的参数。即:

$parsedURL = parse_url($URL);

然后,您使用 parse_str() 函数将数组键 'query' 作为第一个值和第二个值传递,结果将存储在其中。即:

parse_str($parsedURL['query'], $result);

然后,打印 $result 会给你:

Array
(
     [option] => com_lsh
     [view] => lsh
     [event_id] => 149274
     [tid] => 372536
     [channel] => 0
)

http://php.net/manual/en/function.parse-url.phphttp://www.php.net/manual/en/function.parse-str.php

【讨论】:

    猜你喜欢
    • 2016-12-30
    • 2011-03-05
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多