Asked  9 Months ago    Answers:  5   Viewed   60 times

When using PHP with Curl is it necessary to call curl_close() after every call to curl_exec() for cookies to function properly using the CURLOPT_COOKIEJAR AND CURLOPT_COOKIEFILE options? Or can I call curl_exec() as many times as I like to different urls on the same site and still have the cookies maintained without calling curl_close() after each one? Can I use curl_exec() many times and just close it curl_close() at the end of the script?



You should only call curl_close() when you know you're done with that particular handle, or if switching from its current state to a new one (ie: changing a ton of options via curl_setopt() would be faster by going from a clean new handle than your current "dirty" one.

The cookiejar/file options are only strictly necessary for maintaining cookies between seperate curl handles/invokations. Each one's independent of the others, so the cookie files are the only way to share between them.

Wednesday, March 31, 2021
answered 9 Months ago

No you can't. Not from PHP directly.

If you have control over the server you could install phantomjs and have it execute the code since it basically provides you with a headless browser that can do what you want.

Saturday, May 29, 2021
answered 7 Months ago

Probably you was banned at the login process. This will give you more info about the problem:

// change 
// if (!$siteSource) echo 'Help!';
// to
if ($siteSource === false)
    echo 'Curl error: ' . curl_error($curl);

EDIT: some other opt that you can try (SSL related solved my problems more that one time):

curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_VERBOSE, 1);

// following is very important
// TRUE to follow any "Location: " header that the server sends
// as part of the HTTP header (note this is recursive, PHP will follow
// as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

EDIT 2: Following opt will give you the headers returned (use only to debug). Besides be sure about the cookie.txt is being used properly (locatable and writable).

curl_setopt($curl, CURLOPT_HEADER, true);

That's all I can contribute by my side. Now lo lunch!

EDIT 3: Cookie related stuff:

$cookie_file = PATH_TO_YOUR_COOKIE_FILE . '/cookie.txt';

if (! file_exists($cookie_file) || ! is_writable($cookie_file))
    echo 'Cookie file missing or not writable.';

// you already added the following, I put it again just to remark their utility
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);

EDIT 4: Always check this at the beginning:

if ( ! extension_loaded('curl'))
    echo "You need to load/activate the curl extension.";

If you receive this error, activate curl in php.ini uncommenting/removing the front ;

;extension=php_curl.dll // or curl.dll
; // or

and restart the web server. If you do not found this line then you need install curl:

// ubuntu
sudo apt-get install php5-curl

// centos
sudo yum install php5-curl

For windows or your wampserver it must be easy too.

Saturday, May 29, 2021
answered 7 Months ago

CURLOPT_POSTFIELDS as the name suggests, is for the body (payload) of a POST request. For GET requests, the payload is part of the URL in the form of a query string.

In your case, you need to construct the URL with the arguments you need to send (if any), and remove the other options to cURL.

curl_setopt($ch, CURLOPT_URL, $this->service_url.'user/'.$id_user);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);

//$body = '{}';
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
//curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Tuesday, June 29, 2021
answered 6 Months ago

When you want to send cookies, use the CURLOPT_COOKIEFILE option.

When you want to receive cookies and save them, use the CURLOPT_COOKIEJAR option.

If you want to send previously-saved cookies and also update the file with any changes the server makes, use both options.

Saturday, July 31, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :