需要帮助cURL和链接

时间:2015-04-12 04:18:50

标签: php html curl

我正在尝试卷曲以下链接: http://soundstreamradio.serverroom.net:7424/currentsong?sid=1

它只是一个显示纯文本的页面。这是我使用的cURL PHP代码:

function get_web_page( $url )
{
    $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';

    $options = array(

        CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
        CURLOPT_POST           =>false,        //set to GET
        CURLOPT_USERAGENT      => $user_agent, //set user agent
        CURLOPT_COOKIEFILE     =>"cookie.txt", //set cookie file
        CURLOPT_COOKIEJAR      =>"cookie.txt", //set cookie jar
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

这就是我用来调用脚本的原因:


    $result = get_web_page("http://soundstreamradio.serverroom.net:7424/index.html?sid=1");
    if ( $result['errno'] != 0 )
        echo "... error: bad url, timeout, redirect loop ...";
    if ( $result['http_code'] != 200 )
        echo"... error: no page, no permissions, no service ...";
    $page = $result['content'];

现在将这个上传到我的服务器并运行它之后,页面只是挂起并且什么都不做,然后在120秒后超时终止显示所有错误消息(...错误:坏URL,超时,重定向循环......,...错误:没有页面,没有权限,没有服务......)

我正在考虑是否有任何选项可以在没有跨域问题的情况下从本网站提取数据。

2 个答案:

答案 0 :(得分:0)

尝试

echo file_get_contents("http://soundstreamradio.serverroom.net:7424/currentsong?sid=1");

使用curl_exec和其他功能进行https连接

答案 1 :(得分:0)

这对我有用 - 简单明了

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://soundstreamradio.serverroom.net:7424/currentsong?sid=1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

这也有效

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://soundstreamradio.serverroom.net/currentsong?sid=1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_PORT , 7424);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

如果这些都不起作用,那么我会建议您查看防火墙,因为您可能会在端口7424上被阻止