Shoutbox

PHP help - Curl - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP help - Curl (/showthread.php?tid=91092)

PHP help - Curl by Nathan on 06-17-2009 at 04:42 PM

Hey,

Haven't been here for a while but I'm confused on something related to PHP. I've been out of the internet world for about 6-8 months really which means im mentally fucked when it comes to PHP.

code:
<?
function fetch($url,$post=false,$cookie=true,$timeout=1) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
}
if ($post) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, TRUE);
}
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//curl_setopt($ch, CURLOPT_HEADER, TRUE); //debug
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
$cookie=tempnam('','');
$url='http://www.website.com/fsock.php';
$post='username=Nathan1&password=test';
fetch($url,$post,$cookie);

$url='http://www.website.com/vote.php?account=nathan1&site=2';
echo fetch($url,$post,$cookie);

#$url='http://www.website.com/vote/vote.php';
#echo fetch($url,false,$cookie);

?>


Basically it logs in okay, but then when it comes to clicking the link it doesn't work, it redirects to the home page (which after some investigation = not logged in) which makes me think the cookie isn't being stored.
Anyone got any ideas on how to make the cookie last longer or be stored so it'll work?

Thanks :D

RE: PHP help - Curl by WDZ on 06-17-2009 at 06:30 PM

quote:
Originally posted by Nathan
... which makes me think the cookie isn't being stored.
Have you looked at the request headers to make sure you're sending the cookie?

PHP code:
curl_setopt($ch, CURLOPT_VERBOSE, true);


RE: PHP help - Curl by Nathan on 06-17-2009 at 06:38 PM

Okay I tried that, and still no joy


RE: PHP help - Curl by MeEtc on 06-18-2009 at 02:45 AM

set the value of $cookie to a valid filename as a string.


RE: PHP help - Curl by Nathan on 06-18-2009 at 09:43 AM

Can I have an example please?


RE: PHP help - Curl by Ezra on 06-18-2009 at 12:01 PM

Like I already said, don't you have to fopen that file?

I don't think you can just put the filename in curl_setopt...

See: http://nl2.php.net/manual/en/curl.examples-basic.php


RE: PHP help - Curl by Nathan on 06-18-2009 at 12:19 PM

No worries guys, I got it working :)

My code was perfectly fine, it was I was missing a variable that the site needed to allow the vote :D


RE: PHP help - Curl by andrewdodd13 on 06-18-2009 at 09:59 PM

quote:
Originally posted by Ezra
Like I already said, don't you have to fopen that file?

I don't think you can just put the filename in curl_setopt...

See: http://nl2.php.net/manual/en/curl.examples-basic.php
Yeah, you can do this. I found it out earlier. :)