0

Previosly I have been pulling in some page content via CURL from the BBC site using this code,

    $c = curl_init('http://news.bbc.co.uk/sport1/hi/football/eng_conf/conference_north_table/default.stm');

    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $html =  curl_exec ($c);
    if (curl_error($c))
        die(curl_error($c));

    $startpos = strpos($html, '<table border="1" cellpadding="1" cellspacing="0" class="fulltable">');

    $endpos = strpos($html, '<!-- E IINC -->');
    $length = $endpos - $startpos;
    $rest = substr($html, $startpos,$length);
    $rest = str_replace("border=\"1\" cellpadding=\"1\" cellspacing=\"0\"","border=\"0\" cellpadding=\"0\" cellspacing=\"0\"", $rest);
    $rest = str_replace('<tr><td colspan="15"><hr/></td></tr>',"", $rest);

    $rest = str_replace('<tr>
      <td colspan="15">
         <div style="padding: 10px 0 0 0;"><img src=" http://newsimg.bbc.co.uk/sol/shared/img/tbl_spc.gif" height="2px" width="100%"></div>
      </td>
   </tr>
',"", $rest);

    echo $rest;
    curl_close($c);

Previously this was working fine using php 5.1.3 on an old server we have since migrated the site to a server running 5.4.8, however the above code is not working anymore, is there any reason for this? I cannot see any problem.

If I kill the script at $html I get the following reponse,

LEAGUE TABLE

MOVED PERMANENTLY

The document has moved here.

However if I navigate to the URL through the browser I can view the page fine, so it obviously has not moved anywhere (backed up by the fact the Curl request was working before we migrated servers.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Udders
  • 6,914
  • 24
  • 102
  • 194
  • Any error messages? Have you inserted debug statements to see what the values look like? What exactly does "not working anymore" mean? – Olaf Dietsche Nov 15 '12 at 09:16
  • good habit to **always** put block code in `{}` brackets. Even one liners like in your case `if (curl_error($c)) ....` - Such habit would save your from troubles in less expected moment – Marcin Orlowski Nov 15 '12 at 09:20
  • 1
    It may be trying to redirect to another page and not following through. Try http://stackoverflow.com/questions/3519939/make-curl-follow-redirects ? – Maccath Nov 15 '12 at 09:21
  • try curl_info() to see any information – ianace Nov 15 '12 at 09:27

2 Answers2

1

The address has actually moved. Change the first line with:

$c = curl_init('http://news.bbc.co.uk/sport2/hi/football/eng_conf/conference_north_table/default.stm');

The above code now shows me the table with the results.

Plamen Nikolov
  • 2,643
  • 1
  • 13
  • 24
1

You must use

curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);

to follow the redirection.

See PHP: curl_setopt for the full list of options.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198