LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2011, 06:57 AM   #1
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
cURL for php - what can I need it for?


Hello! Please explain me why do I (or someone else) need to use curl in php? They say I can download web-page with it. But we have file_get_contents() for this. Why use curl? I can't understand even after reading info in Web. Just don't get it.
 
Old 09-16-2011, 07:22 AM   #2
PatrickDickey
Member
 
Registered: Sep 2011
Location: Muscatine, IA
Distribution: Ubuntu variants (ubuntu/Mythbuntu) and Windows Home Server/Windows 7
Posts: 33

Rep: Reputation: Disabled
Quote:
Originally Posted by Mr. Alex View Post
Hello! Please explain me why do I (or someone else) need to use curl in php? They say I can download web-page with it. But we have file_get_contents() for this. Why use curl? I can't understand even after reading info in Web. Just don't get it.
The little that I read about this indicates that cURL is faster at downloading the pages. That's about the only real benefit that I've found. This might help you a bit (at least the code example might)

Please note that I'm not a PHP programmer. So, someone with more experience in this language will most likely correct (or clarify) my answer.

Have a great day
Patrick.
 
1 members found this post helpful.
Old 09-16-2011, 07:57 AM   #3
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Original Poster
Rep: Reputation: Disabled
Thanks PatrickDickey!
Someone in the comments said:
"using curl we can post, put and also get. but file_get_contents is only get."

I was searching YouTube to find something interesting about curl and found tutorial about getting info: http://youtu.be/PvEJz6du7R0 . That looks like you must write very different code for each site (maybe depending on it's structure), Alex Garrett doesn't seem to understand curl well. So can someone who codes PHP create a curl code in PHP to show an example how to log into this LQ.org forum and post some message?
 
Old 09-16-2011, 08:20 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Also, a lot of (shared) servers that I've used disable the file_get_contents() function, but you can then use curl to request web pages.

Here's an example from an e-commerce website I built, it checks for stock availability using an enquiry URL:
Code:
function stockAvailable($productModel,$stockType){
      $productModel = substr($productModel,3);
      if($stockType === "MADISON"){
          $ch = curl_init();
          curl_setopt($ch,CURLOPT_URL, "http://www.madisonb2b.co.uk/stockenquiry.aspx?code=".$productModel);
          curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
          curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
          curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
          curl_setopt($ch,CURLOPT_TIMEOUT,10);
          $html=curl_exec($ch);
          if($html==false){
              $m=curl_error(($ch));
              error_log($m);
              echo($m);
              return false;
          }
          curl_close($ch);
          $madisonInStock = htmlspecialchars($html);
          return (substr($madisonInStock,0,3) === "YES");
      } else if($stockType === "DRAPER") {
          if(!file_exists("/tmp/draperStock.csv") || (filemtime("/tmp/draperStock.csv") - time() > 24*60*60)){
              //if draperStock file doesn't exist or is more than a day out of date...
              $ch = curl_init();
              curl_setopt($ch,CURLOPT_URL,"http://www.drapertools.com/products/stock.csv");
              $out = fopen('/tmp/draperStock.csv', 'w');
              curl_setopt($ch, CURLOPT_FILE, $out);
              curl_exec ($ch);
              curl_close ($ch);
              fclose($out);
          }
          $in  = fopen('/tmp/draperStock.csv', 'r');
          while (!feof($in)){
              $buffer = fgets($in, 4096);
              if(substr($buffer,1,5) === $productModel){
                  if(substr($buffer,9,1) === "Y"){
                      return true;
                  }
              }
          }
          fclose($in);
          return false;
      } else if ($stockType === "NONE") {
          return true;
      } else {
          return false;
      }
  }
It uses curl two different ways, one writes it to a file, one just examines the contents of the return result.

EDIT: Very sorry, didn't read your post properly. I'll have a go at your posting problem when I get back from work and Muay Thai. I hope the code I posted helps in some way, though...

Hope this helps,
 
1 members found this post helpful.
Old 09-16-2011, 09:13 AM   #5
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
curl can do SSL and cookie handling, imagine having to log in and handle session authentication cookies to be able to actually retrieve the contents of a deeper page from a site, or submit a restricted (POST) request which could span several pages of interaction manually.

As for having to code specifically for each site, many will have sane features to prevent XSS and CSRF attacks so you'll need to be able to provide things like dynamic hidden token form elements as well the page/request-specific variables/parameters. But I believe that goes for file_get_contents and anything else that's automating/scraping site interaction, not a curl-specific issue.

Last edited by Proud; 09-16-2011 at 09:18 AM.
 
1 members found this post helpful.
Old 09-16-2011, 04:26 PM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Had a brief go at it; I was prototyping in Python, if you want the code then I will post it. However, the real challenge is not how to use curl, it's the sending of the correct headers and contents (as Proud mentioned), so my writing code to log into LQ will not really help you with your problem, unless your problem is to spam LQ :P

If you are trying to log in automatically to a site, you're going to need to have some software to look at the headers/content that your browser is sending - I've used fiddler before (on windows) and the 'Net' panel in firebug (on Linux)

Hope this helps,
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Recompile PHP with CURL stranger_6_7 Linux - General 1 01-11-2007 12:14 AM
Curl under PHP ttxmika Slackware 11 05-31-2005 11:24 AM
PHP/cURL housemusic42 Linux - Networking 0 10-07-2004 02:45 PM
Curl and PHP...? hurieka Linux - Software 6 09-28-2004 11:14 PM
php and curl Risen Linux - Software 0 09-02-2003 03:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration