0

I have to crawl some values from a website. Should I use curl for that or file_get_contents ??

I am getting some warning with file_get_contents at my localhost .

Any help will be appreciated

2
  • 1
    Can you provide a short example of your code? Commented Dec 26, 2011 at 14:29
  • 2
    What warning are you getting? Commented Dec 26, 2011 at 14:30

3 Answers 3

3

If you have basic requirements, I would favor file_get_contents. If you need to set headers, and request method etc... I would recommend using curl.

In your case, I think file_get_contents is enough.

Alternatively, you can use file which returns an array of lines from the retrieved file. It works with locally accessible files, and also with remote urls. I often find it more convenient to loop over an array of lines, than to deal with the whole file in one block - so this might be your best option.

<?php
  foreach(file('http://example.com/the-file.ext') as $line){
    // do something with $line
  }
?>
Sign up to request clarification or add additional context in comments.

Comments

1

I think Curl is more preferable, as compared to file_get_contents as you can set headers, request methods like POST or GET, follow redirection etc. So, curl will be advisble

<?php
$ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    echo $data;
?>

Comments

0

file_get_contents should be just fine for that purpose

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.