POST in cURL lib with PHP does not send

I am trying to send POSTDATA via cURL with PHP, and I think the message is not sending

$ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_URL, $url); $t = curl_exec($ch); 

I don’t know why, but I try to enter the page and when I dump $ t, I always see the form, curl_error is empty, what can I do to debug this? The page trying to log in is not mine!


I have a local form emulating cURL and its OK:

 <form action="$url" method="POST"> <input type="hidden" name="username" value="$uspw" /> <input type="hidden" name="password" value="$uspw" /> <input type="submit" value="Send" /> </form> 

It will start in $ url! Here is the $ data that I send POSTFIELDS to cURL

 $data = array('username' => $uspw, 'password' => $uspw); 

Yes .. user and password are the same


Headers Received:

 HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Sun, 25 Nov 2012 05:23:19 GMT Server: Apache Cache-Control: no-cache="set-cookie" Content-Length: 4822 Set-Cookie: SESSIONID_portalApp=xxxxxx!-xxx!xxx; path=/ Content-Language: en X-Powered-By: Servlet/2.4 JSP/2.0 Content-Type: text/html; charset=ISO-8859-1 

and after that I get the whole form, again ...


 $data = array('username' => $uspw, 'password' => $uspw); $header = array('Origin: xxx', 'Content-Type: application/x-www-form-urlencoded', 'Connection: keep-alive', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Cache-Control: max-age=0', 'Except:', 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Accept-Encoding:gzip,deflate,sdch', 'Accept-Language:es-ES,es;q=0.8', ); $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLINFO_HEADER_OUT, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FORBID_REUSE, 0); curl_setopt($ch, CURLOPT_FRESH_CONNECT, 0); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11"); curl_setopt($ch, CURLOPT_COOKIEJAR, 'xxx.txt'); curl_setopt($ch, CURLOPT_REFERER, 'xxxx'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_URL, $url); $t = curl_exec($ch); 

Thanks!

+4
source share
2 answers

It looks like you are trying to log in to a third-party system without a browser.

It is very likely that this does not work because you are not doing what it expects to create a session cookie. Although you can log in correctly, the remote site will probably show you the form again because nothing in your cookie reports this when redirecting. The headings explain this:

Set-Cookie: SESSIONID_portalApp = xxxxxx! -xxx! xxx; Path = /

Create a cookie to use cURL (using something like tempnam() ) and tell cURL where it lives using the CURLOPT_COOKIEJAR directive. You can also grab an existing cookie from your browser and try as an additional way to troubleshoot.

Add this before initializing cURL:

 $cookieFile = tempnam('/dev/shm', 'curlCookie_'); 

Then say cURL to use it:

 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); 

I suggested /dev/shm because it was erased during reboot, you can create it wherever you have the opportunity to do this. Just make sure to remove them when they are no longer needed. You can also change the prefix to make it easier to map individual files to individual requests (timestamp, PID, etc.), which are convenient for debugging.

Also make sure that you set the user agent string to something known as acceptable to the remote site. Remember, it expects a browser - so you have to be sure that you behave as one. Setting CURLOPT_FOLLOWLOCATION true is probably a good idea, as suggested in the comments.

If this does not work, follow the steps in your browser and look carefully at the pages. Maybe JS is required to properly build the session. At this point, you are just out of luck just using cURL.

+1
source

Visit the form page, and then, with the same curl resource, complete the submit request. In addition, add the following settings:

 curl_setopt($ch, CURLOPT_FORBID_REUSE, 0); curl_setopt($ch, CURLOPT_FRESH_CONNECT, 0); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_USERAGENT, "valid user agent"); 
0
source

All Articles