I’m having a little difficulty in figuring out how to run multiple cURL requests, and I’m not entirely sure what I’m doing wrong. It may be that my code for logging in via the webform that I’ve written is incorrect, but its difficult to test that on its own as I do need to run the first query before I can do that.
This is the code I have for my first query;
$handle = curl_init();
$login_user = 'user';
$login_pass = 'pass';
$username = 'username';
$password = 'password';
$postdata = "username=".$username."&password=".$password;
curl_setopt($handle, CURLOPT_URL, 'http://somewebsiteexample.php');
curl_setopt($handle, CURLOPT_USERPWD, $login_user . ":" . $login_pass );
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($handle);
curl_close($handle);
echo '<pre>';
print_r($response);
?>
Which gets me past the basic http authentication. Now I need to be able to log in to the site by posting the request to the webform php page.
I’ve seen plenty of examples of how to do this, but only on their own, and not as a second cURL request.
I have all the information I need to be able to do each individual request, but I’m not sure how to combine the two requests together. Do I do it in the same cURL session?
I get the feeling that I do need to do seperate curl initialisation requests and that I need to store the login information from the first request via a cookie using CURLOPT_COOKIEJAR. (Or can I run my second query in the same curl_init()?)
If that is the case, how do I store the basic http authentication request so that when I next initialise curl, I am already logged into the website where I can then POST to the webform? I can probably figure out the rest once I know how to do multiple cURL requests.
I’m fairly new to cURL (I still use file_get_contents() for web pages a lot of the time), so I’m also a little unsure on which cURL options are absolutely necessary for logging in via webform. Some seem quite obvious, whereas others do not. This is something I can probably figure out on my own, but its hard to find a solid tutorial for logging in with cURL that has a breakdown of what each CURLOPT constant does in that particular context.
The php.net manual has a lot of information, and while it does tell you what the various cURL options relate to, it doesn’t provide specific examples of their specific usages. If there is another resource site where I can get a more in-depth explanation of cURL, that would also be appreciated.
You need to look into ways curl is debugged. Curl naturally won’t trace anything, you need to call for
curl_error($curl)andcurl_errno($curl)after the request, but before the resource is closed withcurl_close($curl).You also need to trace
curl_getinfo($curl)for details regarding request and response. Make sure your blank page is not caused by syntax errors in your script.Next part. Add
CURLOPT_COOKIEJARandCURLOPT_COOKIEFILEfor cookies handling. Do the second request on the same curl resource, runcurl_close($curl)after all requests have been done. Curl saved most of the setup between requests, so don’t worry about that.I would also suggest adding user agent and referer settings to make your request look more natural.