0

ページから特定の行を取得するように求められましたが、サイトがCURLリクエストをブロックしているようです。

問題のサイトはhttp://www.habbo.com/home/Intricatです。

UserAgentを変更して、それをブロックしているかどうかを確認しようとしましたが、うまくいかなかったようです。

私が使用しているコードは次のとおりです。

<?php

$curl_handle=curl_init();
//This is the URL you would like the content grabbed from
curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($curl_handle,CURLOPT_URL,'http://www.habbo.com/home/Intricat');
//This is the amount of time in seconds until it times out, this is useful if the server you are requesting data from is down. This way you can offer a "sorry page"
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);

curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
//This Keeps everything running smoothly
curl_close($curl_handle);

// Change the message bellow as you wish, please keep in mind you must have your message within the " " Quotes.
if (empty($buffer))
{
    print "Sorry, It seems our weather resources are currently unavailable, please check back later.";
}
else
{
    print $buffer;
}
?>

CURLリクエストをブロックした場合に、そのページからコード行を取得できる別の方法に関するアイデアはありますか?

編集:サーバーでcurl -iを実行すると、サイトが最初にCookieを設定しているように見えますか?

4

4 に答える 4

1

ブラウザを使用して、送信されているヘッダーを正確にコピーします。リクエストはまったく同じように見えるため、サイトはあなたがカールしようとしていることを認識できません。Cookieを使用する場合は、Cookieをヘッダーとして添付します。

于 2012-11-02T16:37:32.250 に答える
1

これは、私がかなり数年前に行ったCurlクラスからのカットアンドペーストです。あなたが自分でそれからいくつかの宝石を選ぶことができることを願っています。

function get_url($url)
{ 
    curl_setopt ($this->ch, CURLOPT_URL, $url); 
    curl_setopt ($this->ch, CURLOPT_USERAGENT, $this->user_agent);
    curl_setopt ($this->ch, CURLOPT_COOKIEFILE, $this->cookie_name);
    curl_setopt ($this->ch, CURLOPT_COOKIEJAR, $this->cookie_name);
    if(!is_null($this->referer))
    {
        curl_setopt ($this->ch, CURLOPT_REFERER, $this->referer);  
    }
    curl_setopt ($this->ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt ($this->ch, CURLOPT_HEADER, 0); 
    if($this->follow)
    {
        curl_setopt ($this->ch, CURLOPT_FOLLOWLOCATION, 1);
    }
    else
    {
        curl_setopt ($this->ch, CURLOPT_FOLLOWLOCATION, 0);
    }
    curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($this->ch, CURLOPT_HTTPHEADER, array("Accept: text/html,text/vnd.wap.wml,*.*"));
    curl_setopt ($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // this line makes it work under https

    $try=0;
    $result="";
    while( ($try<=$this->retry_attempts) && (empty($result)) )  // force a retry upto 5 times
    {
        $try++;
        $result = curl_exec($this->ch);
        $this->response=curl_getinfo($this->ch);
        // $response['http_code'] 4xx is an error
    }
    // set refering URL to current url for next page.
    if($this->referer_to_last) $this->set_referer($url);

    return $result; 
}
于 2012-11-02T16:38:04.583 に答える
1

あなたはあなたが話しているブロックの種類についてあまり具体的ではありません。問題のWebサイトは、http://www.habbo.com/home/Intricatまずブラウザでjavascriptが有効になっているかどうかを確認します。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
    }
    function getHostUri() {
        var loc = document.location;
        return loc.toString();
    }
    setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', '179.222.19.192', 10);
    setCookie('DOAReferrer', document.referrer, 10);
    location.href = getHostUri();</script>
</head>
<body>
<noscript>This site requires JavaScript and Cookies to be enabled. Please change your browser settings or upgrade your
    browser.
</noscript>
</body>
</html>

curlはjavascriptをサポートしていないため、HTTPクライアントを使用する必要があります。または、そのスクリプトを模倣して、独自のCookieと新しいリクエストURIを作成する必要があります。

于 2012-11-02T16:38:37.223 に答える
0

これはとても古い投稿だと思いますが、今日も同じ質問に答えなければならなかったので、ここで来てくれる人たちのために共有しますので、役に立つかもしれません。私はまた、OPがcurl具体的に求めていることを十分に承知していますが、私と同じように、解決策に興味を持っている人がいるかどうcurlかに関係なく存在する可能性があります。

取得したいページがcurlブロックされました。ブロックが理由ではなくjavascript、エージェントが原因である場合(これは私の場合であり、エージェントを設定しても効果curlがありませんでした)、wget解決策になる可能性があります。

wget -o output.txt --no-check-certificate --user-agent="Mozilla/5.0 (Windows NT 5.2; rv:2.0.1) Gecko/20100101 Firefox/4.0.1" "http://example.com/page"
于 2015-08-10T22:20:34.780 に答える