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));
}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;
}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]