Why does this function use CURL for some URLs, but not for others?

I am writing a website in PHP that aggregates data from other sites. I have a returnPageSource function that takes a url and returns html from that url as a string.

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;
}

This works great for some websites that I need, for example http://atensembl.arabidopsis.info/Arabidopsis_thaliana_TAIR/unisearch?species=Arabidopsis_thaliana_TAIR;idx=;q=At5g02310 , but not for others, for example http: // www .bar.utoronto.ca / efp / cgi-bin / efpWeb.cgi? dataSource = Chemical & modeInput = Absolute & primaryGene = At5g02310 & orthoListOn = 0 . Does anyone know why?

Update

. useragent , (Firefox 3, ), 0, , . curl_error() " " curl_getinfo ($ ch, CURLINFO_HTTP_CODE); HTTP 0... . curl_setopt ($ ch, CURLOPT_VERBOSE, 1);, . - ?

, , - ( ). !

+3
3
+6

, 0.

HTTP ? curl_getinfo($ch, CURLINFO_HTTP_CODE);.

- , , User-Agent, , , , . , .

http- .

Edit:

. , - CURLOPT_CONNECTTIMEOUT.

, script (), , ( ). , . 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>

? . , ( , script -). - , , , (, , ). , PHP ?

+1

, .

-, - . 5 -.

Secondly, the sites in question may intentionally block your request. They have a rule to block requests coming from curl, or they might notice suspicious activity (either screen clearing or other network abuse) coming from your IP address and blocking / throttling of requests.

+1
source

Source: https://habr.com/ru/post/1703248/


All Articles