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.

4
  • Any error messages? Have you inserted debug statements to see what the values look like? What exactly does "not working anymore" mean? Commented Nov 15, 2012 at 9: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 Commented Nov 15, 2012 at 9:20
  • 1
    It may be trying to redirect to another page and not following through. Try stackoverflow.com/questions/3519939/make-curl-follow-redirects ? Commented Nov 15, 2012 at 9:21
  • try curl_info() to see any information Commented Nov 15, 2012 at 9:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You must use

curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);

to follow the redirection.

See PHP: curl_setopt for the full list of options.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.