cURL vs file_get_contents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ario
Forum Newbie
Posts: 13
Joined: Wed Apr 26, 2006 3:56 pm

cURL vs file_get_contents

Post by ario »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've been developing a site for a few months which I just today uploaded to my host site.  I've been using file_get_contents to read in data from a remote site, but my host has allow_url_fopen disabled for security reasons, so I've been learning cURL.  I'm basically using these functions to simulate a user logging in, and then storing those cookies and transmitting them whenever I request a page so I can access content that's restricted to logged-in users.  So, I've been translating my code from using file_get_contents to the curl functions.  The function that logs in and then stores the cookies is working correctly--but then when I try to access a page, I get redirected to the login page by the site, with the error message "There has been an error during logon. To prevent this error in the future, please scan your computer for spyware. A SECOND LOGON WILL WORK."

Here's the file_get_contents code, which does work, but won't run on my host:

Code: Select all

function getPage($url)
{
	// $cookiesToSend is an array of cookies stored as strings like "cookiename=cookieval"
	// which store the login data that must be transmitted to get pages
	global $cookiesToSend;
	
	$cookieHeaders = array(
		'http' => array(
			'method' => 'GET',
			'header' => 'Cookie: '.implode('; ', $cookiesToSend)
			)
	);
	
	return file_get_contents($url, false, stream_context_create($cookieHeaders));
}
Here's the cURL code, which gives the error message:

Code: Select all

function getPage($url)
{
	global $cookiesToSend;
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_COOKIE, implode('; ', $cookiesToSend));
	$contents = curl_exec($ch);
	curl_close($ch);
	return $contents;
}
I'm not sure if setting CURLOPT_SSL_VERIFYPEER to false is affecting this, but when I don't set it, I get null content. The pages I am accessing are https addresses. Any ideas, anyone?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
the DtTvB
Forum Newbie
Posts: 11
Joined: Sun Feb 11, 2007 6:10 am

Re: cURL vs file_get_contents

Post by the DtTvB »

ario wrote:I'm not sure if setting CURLOPT_SSL_VERIFYPEER to false is affecting this, but when I don't set it, I get null content.
So yes, it affects.
ario wrote:The pages I am accessing are https addresses. Any ideas, anyone?
That's why it affects.

:arrow:   Everything seems right to me.
User avatar
ario
Forum Newbie
Posts: 13
Joined: Wed Apr 26, 2006 3:56 pm

Post by ario »

>> I'm not sure if setting CURLOPT_SSL_VERIFYPEER to false is affecting this, but when I don't set it, I get null content.

So yes, it affects.
Sorry, let me clarify my pronouns... by "this" I meant "the problem I am having", not "the entire process". So obviously, that setting affects the process, but as I said, when I leave that setting out, I just get a blank page, so I need it there. So, let me rephrase: I am not sure if setting CURLOPT_SSL_VERIFYPEER to false is responsible for the error message I am receiving, but so far I've found it necessary in order to get any content at all, so I feel like there must be some other setting that I need to change or set.

And yes, the code I've used does look correct, but obviously there's some setting that file_get_contents is handling that curl is not.

Is there any way that I can have both of the methods (file_get_contents and curl) print out whatever headers they're transmitting, so I can compare them?[/quote]
User avatar
ario
Forum Newbie
Posts: 13
Joined: Wed Apr 26, 2006 3:56 pm

Post by ario »

Never mind. I'd hard-coded the login page URL into the script instead of taking the argument from the function. I can't believe I did that and it took me so long to figure it out.
Post Reply