4

他のさまざまな Web サイトからのデータを集約する Web サイトを PHP で作成しています。URLを受け取り、そのURLからhtmlを文字列として返す関数「returnPageSource」があります。

function returnPageSource($url){
    $ch = curl_init();
    $timeout = 5;   // set to zero for no timeout       

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     // means the page is returned
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOUT_CONNECTTIMEOUT, $timeout); // how long to wait to connect
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     // follow redirects
    //curl_setopt($ch, CURLOPT_HEADER, False);          // only request body

    $fileContents = curl_exec($ch); // $fileContents contains the html source of the required website
    curl_close($ch);

    return $fileContents;
}

これは、 http://atensembl.arabidopsis.info/Arabidopsis_thaliana_TAIR/unisearch?species=Arabidopsis_thaliana_TAIR;idx=;q=At5g02310のように、私が必要とするいくつかのウェブサイトではうまく機能します が、 http://www.barのような他のウェブサイトでは機能しません.utoronto.ca/efp/cgi-bin/efpWeb.cgi?dataSource=Chemical&modeInput=Absolute&primaryGene=At5g02310&orthoListOn=0 . 誰かが理由を知っていますか?

アップデート

回答ありがとうございます。ユーザー エージェントをブラウザー (サイトに正常にアクセスできる Firefox 3) と同じになるように変更し、タイムアウトを 0 に変更しましたが、まだ接続できませんが、いくつかのエラー メッセージが表示されます。curl_error() で「ホストに接続できませんでした」というエラーが表示され、curl_getinfo($ch, CURLINFO_HTTP_CODE); HTTP コード 0 を返します...どちらもあまり役に立ちません。curl_setopt($ch, CURLOPT_VERBOSE, 1); も試しましたが、何も表示されませんでした。他のアイデアはありますか?

最終更新

何が問題なのかを説明していないことに気付きました。大学のプロキシ設定を入力する必要がありました (大学のサーバーを使用しています)。その後、すべてがうまくいきました!

4

3 に答える 3

6

どのエラーが発生したかを確認するために使用curl_error()する必要があります (存在する場合)

于 2009-02-15T22:07:36.317 に答える
1

考慮すべき2つのこと。

1つ目は、タイムアウトを低く設定したことです。これらのウェブサイトでは、リクエストに5秒以上かかる場合があります。

2つ目は、問題のWebサイトが意図的にリクエストをブロックしている可能性があることです。彼らはcurlからのリクエストをブロックするルールを持っているか、あなたのIPアドレスからの疑わしいアクティビティ(画面のスクレイピングまたは他の誰かのネットワークの悪用)に気づき、リクエストをブロック/抑制している可能性があります。

于 2009-02-15T22:32:21.110 に答える
1

タイムアウトを0に設定しようとしたと思います。

これらのサイトが返す HTTP ステータス コードは何ですか? 確認してくださいcurl_getinfo($ch, CURLINFO_HTTP_CODE);

これらのページにアクセスするために機能することがわかっているので、おそらくあなた自身のブラウザのヘッダーで User-Agent ヘッダーをスプーフィングすることも試してみてください。ボットがページにアクセスするのを止めようとしているだけかもしれません。

ヘッダーと http コードを調査すると、もう少し情報が得られるはずです。

編集:

私はこれをもう少し調べました。1 つのことは、接続タイムアウトのタイプミスがあることCURLOPT_CONNECTTIMEOUTです。

とにかく、私はあなたが探しているものを返すこのスクリプト(以下)を実行しました(私は思う)。それとあなたの違いを確認してください。役立つ場合は、PHP 5.2.8 を使用しています。

<?php

$addresses = array(
    'http://atensembl.arabidopsis.info/Arabidopsis_thaliana_TAIR/unisearch?species=Arabidopsis_thaliana_TAIR;idx=;q=At5g02310',
    'http://www.bar.utoronto.ca/efp/cgi-bin/efpWeb.cgi?dataSource=Chemical&modeInput=Absolute&primaryGene=At5g02310&orthoListOn=0'
);

foreach ($addresses as $address) {
    echo "Address: http://www.bar.utoronto.ca/efp/cgi-bin/efpWeb.cgi?dataSource=Chemical&modeInput=Absolute&primaryGene=At5g02310&orthoListOn=0\n";
    // This box doesn't have http registered as a transport layer - pfft
    //var_dump(fsockopen($address, 80));

    $ch = curl_init($address);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

    $fc = curl_exec($ch);

    echo "Info: " . print_r(curl_getinfo($ch), true) . "\n";

    echo "$fc\n";

    curl_close($ch);
}

次を返します(TL;DR:私のcURLはページを正常に読み取ることができます):

C:\Users\Ross>php -e D:\sandbox\curl.php

Address: http://www.bar.utoronto.ca/efp/cgi-bin/efpWeb.cgi?dataSource=Chemical&modeInput=Absolute&primaryGene=At5g02310&orthoListOn=0

Info: Array
(
    [url] => http://atensembl.arabidopsis.info/Arabidopsis_thaliana_TAIR/unisearch?species=Arabidopsis_thaliana_TAIR;idx=;q=At5g02310
    [content_type] => text/html; charset=ISO-8859-1
    [http_code] => 200
    [header_size] => 168
    [request_size] => 151
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.654
    [namelookup_time] => 0.004
    [connect_time] => 0.044
    [pretransfer_time] => 0.044
    [size_upload] => 0
    [size_download] => 7531
    [speed_download] => 11515
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 0.57
    [redirect_time] => 0
)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb"  lang="en-gb">
<head>
  <title>AtEnsembl release 49: Arabidopsis thaliana TAIR EnsEMBL UniSearch results</title>
  <style type="text/css" media="all">
    @import url(/css/ensembl.css);
    @import url(/css/content.css);
  </style>
  <style type="text/css" media="print">
    @import url(/css/printer-styles.css);
  </style>
  <style type="text/css" media="screen">
    @import url(/css/screen-styles.css);
  </style>
  <script type="text/javascript" src="/js/protopacked.js"></script>
  <script type="text/javascript" src="/js/core42.js"></script>
  <!-- Snipped for freedom - lots of lines -->
</body>
</html>

Address: http://www.bar.utoronto.ca/efp/cgi-bin/efpWeb.cgi?dataSource=Chemical&modeInput=Absolute&primaryGene=At5g02310&orthoListOn=0

Info: Array
(
    [url] => http://www.bar.utoronto.ca/efp/cgi-bin/efpWeb.cgi?dataSource=Chemical&modeInput=Absolute&primaryGene=At5g02310&orthoListOn=0
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 146
    [request_size] => 155
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 2.695
    [namelookup_time] => 0.004
    [connect_time] => 0.131
    [pretransfer_time] => 0.131
    [size_upload] => 0
    [size_download] => 14156
    [speed_download] => 5252
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 2.306
    [redirect_time] => 0
)

<html>
<head>
  <title>Arabidopsis eFP Browser</title>
  <link rel="stylesheet" type="text/css" href="efp.css"/>
  <link rel="stylesheet" type="text/css" href="domcollapse.css"/>
  <script type="text/javascript" src="efp.js"></script>
  <script type="text/javascript" src="domcollapse.js"></script>
</head>
<body>
<!-- SANITY SNIP -->
</body>
</html>

これはどういう意味ですか?完全にはわかりません。彼らがあなたを具体的にブロックしているとは思えません (このスクリプトを Web サーバーで実行していない限り、ページにアクセスできるため)。上記のコードを実行してみてください - それが機能する場合は、コードの一部をコメントアウトして、違いを確認してください (停止の原因となる可能性があります)。また、実行しているPHPのバージョンは何ですか?

于 2009-02-15T22:00:04.947 に答える