ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Distribution: Ubuntu variants (ubuntu/Mythbuntu) and Windows Home Server/Windows 7
Posts: 33
Rep:
Quote:
Originally Posted by Mr. Alex
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.
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?
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...
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.
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.