Is it possible to set multiple curl_setopt only once and re-use them in future curl exec's as long as the curl handle is not closed ? (Especially Useragent and the Cookie)
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 Gecko/20100101 Firefox/49.0");
curl_setopt($curl, CURLOPT_COOKIE, "PHPSESSID=".session_id());
curl_setopt($curl, CURLOPT_URL, "https://foo.bar/action/");
$ret = curl_exec($curl);
## DO SOME STUFF ##
curl_setopt($curl, CURLOPT_URL, "https://foo.bar/anotherAction/"); // Set only new URL ..
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // ... and the POST data.
$ret2 = curl_exec($curl);
curl_close($curl);
?>
Solution is as simple as the question .. just do it the way I did in the question. As long as the cURL session is not closed by curl_close();, you can query as many requests as you like with the headers of your choice only set once after curl_init();.
Related
$url = 'https://api.stackexchange.com/2.2/answers/'.$id.'?order=desc&sort=activity&site=stackoverflow&filter=!-*f(6t*ZdXeu&key=MY_KEY';
gzdecode(file_get_contents ($url)) ;
this caused me problems today when I played with stackoverflow APIs
alternative approach - just use CURL, it does decompression automatically
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 400);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the url changes
curl_setopt($curl, CURLOPT_MAXREDIRS, 5); //if http server gives redirection responce
curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // the page encoding
$data = curl_exec($curl); // data already decompressed
curl_close($curl);
I have installed curl. and configuration on my system is
cURL support enabled
cURL Information 7.35.0
Age 3
I have write code in my project is :
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($curl, CURLOPT_SSLVERSION,6);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_URL, $submit_url);
$curl_execute = curl_exec($curl);
curl_close($curl);
Its not giving me value on my local machine while working fine on server. What is problem?
My PHP version is
5.5.9-1ubuntu4.5
I am using ubuntu 13.10
what is problem with my local machine? what i am missing. please suggest what to do?
Edit :
If i missed any information to write, please inform me.. i am using curl very first time.
At first define the URL and post values correctly, I have added examples, please try this:
// define url examples
$submit_url = 'http://localhost/phptest/service.php';
// post fields examples
$postfields = 'name=foo&pass=bar&format=json';
// Start the process:
$curl = curl_init();
// Tell cURL to fail if an error occurs:
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
// Allow for redirects:
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
// Assign the returned data to a variable:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Set the timeout:
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
// Use POST:
curl_setopt($curl, CURLOPT_POST, 1);
// Set the POST data:
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_URL, $submit_url);
// Execute the transaction:
$result = curl_exec($curl);
// Close the connection:
curl_close($curl);
// Print the results:
print_r($result);
I'm writing a function to fetch the captcha, of course it needs to keep the session. I'm using cookie file for curl and use it for every request but it does not work. When I view the cookie file, I see that the PHPSESSID changed each time I call the function. How could I solve it?
Here is my code
<?php
function fetch_captcha($url, $cookie_file = false, $user_agent = DEFAULT_USER_AGENT, $timeout = 10) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
if ($cookie_file) {
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
}
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Don't use:
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
PHPSESSID is a session cookie, and this option causes each cURL call to start a new session, so it ignores all session cookies in the file.
I have a problem with my requests with cURL.
I want to log-in, it works. I want to conserve the cookie to keeep the connexion available, it works.
$lien = 'https://thewebsite.com';
$postfields = array(
'username' => 'test123',
'password' => 'test123'
);
$path_cookie = 'connexion.txt';
if (!file_exists(realpath($path_cookie))) touch($path_cookie);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lien);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEJAR, realpath($path_cookie));
$return = curl_exec($curl);
echo($return);
curl_close($curl);
Second part :
$lien2 = 'https://thewebsite.com/myaccount';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lien2);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEFILE, realpath($path_cookie));
$return = curl_exec($curl);
echo realpath($path_cookie);
curl_close($curl);
But when I want to make an other requests, it won't work, the output is :
Object moved to here.
Here is the page of the login (https://thewebsite.com) ...
So the connexion doesn't stay available and the server has been kicked out when I try to achieve the second curl command.
Any one can help me please?
Maybe the first request isn't complete before the second one, how can I make a pause between the 2 requests? (sleep won't work)
Taken from php documentation:
CURLOPT_COOKIESESSION:
TRUE to mark this as a new cookie "session". It will force libcurl to ignore all cookies it is about to load that are "session cookies" from the previous session. By default, libcurl always stores and loads all cookies, independent if they are session cookies or not. Session cookies are cookies without expiry date and they are meant to be alive and existing for this "session" only.
So in other words, remove CURLOPT_COOKIESESSION from your second part code and your code should work.
I try to login with curl to a SSL secured website but somehow I don't get it right.
The first curl connection retrieves the login form. An SSL issue in the beginning is resolved now. The fields used for authentication and all hidden fields are identified and used for the following POST. The cookie file is defined and the jar to read from as well. The cookie file is accessible and gets updated with each login attempt. A session cookie is successfully set by curl. The HTTPHEADER is removed to stop the request from hitting the 100 Continue wall. Curl is configured to follow up and to send a referer.
However, I still cannot find where the script gets stuck. Neither Curl nor PHP issue any error messages or warnings.
Here is the shortened script:
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); // remove Expect header to avoid 100 Continue situations
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla [abbreviated]');
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__).'/cacert.pem');
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.hq.txt'); // write cookies
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.hq.txt'); // read cookies
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_URL, 'https://the_url.jsp');
$data = curl_exec($ch);
$error= curl_error($ch);
if(!empty($error))
echo '<p>'.$error.'</p>';
else
echo '<p>ok</p>';
Now the script reads the form, fills in the credentials and POSTs it back using the same curl_init handle:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $options);
$data = curl_exec($ch);
$error=curl_error($ch);
But all I get back is the same form again and the same session cookie or a new one depending on the CURLOPT_COOKIESESSION setting.
When I log in manually I notice that there are two more cookies set: LtpaToken and LtpaToken2 but I never see them appearing in the request headers printed by the script.
Manually submitting the form works even without Javascript aktivated. So there cannot be some JS magic modifying the form data under the hood before submitting it.
Obviously I am missing something here. Any ideas where I can look further?
Solved: Finally the issue was due to an encoding issue in the POST.
Initially the POST data was created from an array with http_build_query().
Now the POST data is simply concatenated and both keys and values are urlencoded separately:
$options.=urlencode($fieldName).'='.urlencode($element->getAttribute('value'));
Solved: Finally the issue was due to an encoding issue in the POST. Initially the POST data was created from an array with http_build_query(). Now the POST data is simply concatenated and both keys and values are urlencoded separately:
$options.=urlencode($fieldName).'='.urlencode($element->getAttribute('value'));
See below url:--
cURL login session
and try this:-
http://php.net/manual/en/function.curl-setopt.php
<?php
echo curl_grab_page("https://www.example.net/login.php", "https://www.example.net/", "username=foo&password=bar", "true", "null", "false");
// $url = page to POST data
// $ref_url = tell the server which page you came from (spoofing)
// $login = true will make a clean cookie-file.
// $proxy = proxy data
// $proxystatus = do you use a proxy ? true/false
function
curl_grab_page($url,$ref_url,$data,$login,$proxy,$proxystatus){
if($login == 'true') {
$fp = fopen("cookie.txt", "w");
fclose($fp);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if ($proxystatus == 'true') {
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $ref_url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
ob_start();
return curl_exec ($ch); // execute the curl command
ob_end_clean();
curl_close ($ch);
unset($ch);
}