jeudi 9 avril 2015

how to do 'form submit' manually using PHP cURL?

Here is what i did using cURL in PHP:


1) Login to website: http://example.com and fetch the resulting page using:



$fp = fopen("cookie.txt", "w");
fclose($fp);
$login = curl_init();
curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($login, CURLOPT_TIMEOUT, 40000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($login, CURLOPT_URL, "http://main.com");
curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($login, CURLOPT_POST, TRUE);
curl_setopt($login, CURLOPT_POSTFIELDS, "id=myID&pw=myPW");
ob_start();
return curl_exec ($login);
ob_end_clean();
curl_close ($login);
unset($login);


2) and the page I fetched as a result is in HTML format:



<form id="form" action="http://ift.tt/1Ebcg6b" method="POST">
<select name="mySelect">
<option value="1" selected>one</option>
<option value="2" selected>two</option>
<option value="3" selected>three</option>
</select>
<input type="submit" value="SUBMIT" />
</form>


3) I click on "SUBMIT" button to submit from the above html


4) I get redirected to a different page which resulted from submitting the form.


But here, I want to extract that different page instead of being redirected to by performing 'submit' manually with php. rather than clicking on the SUBMIT button.


So when I tried to do that with cURL, the problem i got was that POST was done as if I wasn't logged in. I need to keep that logged-in state in order to do POST action with cURL. Is there a way to do this? probably by keeping cookie alive, or session alive for the second POST action?


Aucun commentaire:

Enregistrer un commentaire