Simulating a POST with PHP & cURL -
i'm trying simulate post website based on see coming live http headers in firefox.
here's copy/paste of log firefox plugin:
post /context?tab=login http/1.1
host: website
user-agent: mozilla/5.0 (x11; u; linux i686; en-us; rv:1.9.2.13) gecko/20101206 ubuntu/10.10 (maverick) firefox/3.6.13
accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
accept-language: en-us,en;q=0.5
accept-encoding: gzip,deflate
accept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.7
keep-alive: 115
connection: keep-alive
referer: referer
cookie: fontsize=2; jsessionid=0000pxe_bk7tjzfzeynhqokzxz2:-1
content-type: application/x-www-form-urlencoded
content-length: 46
loginid=password&password=password&login=login
and response follows after post:
http/1.1 302 found
location: website/context?tab=p00689
content-language: en-us
set-cookie: jsessionid=0000oaklieedrwkx5yciju5v1lm:-1; path=/
transfer-encoding: chunked
date: mon, 07 feb 2011 14:15:21 gmt
server: websphere application server/6.1
expires: thu, 01 dec 1994 16:00:00 gmt
cache-control: no-cache="set-cookie, set-cookie2"
based on testing, response redirects
website/context?tab=p00689
means user authenticated , worked properly.
however, when trying accomplish via php & curl, i'm being redirected page informs user session has timed out.
here's code:
// provider likes firefox $agent = "user-agent: mozilla/5.0 (x11; u; linux i686; en-us; rv:1.9.2.13) gecko/20101206 ubuntu/10.10 (maverick) firefox/3.6.13"; ini_set("user_agent", $agent); // cookie $cookie = tempnam("/tmp", "curlcookie"); // post posted me. $fields = $_post; foreach($fields $key=>$value) { $fields_string .= "$key=$value&"; } $fields_string = substr($fields_string, 0, strlen($fields_string) - 1); // custom headers $headers = array( "accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "accept-language: en-us,en;q=0.5", "accept-encoding: gzip,deflate", "accept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.7", "keep-alive: 115", "connection: keep-alive"); // curl options $ch = curl_init("website"); curl_setopt($ch, curlopt_referer, "referer"); curl_setopt($ch, curlopt_header, 1); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_postfields, $fields_string); curl_setopt($ch, curlopt_cookiejar, $cookie); curl_setopt($ch, curlopt_cookiefile, $cookie); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); curl_setopt($ch, curlopt_followlocation, false); curl_setopt($ch, curlopt_maxredirs, 1); curl_setopt($ch, curlinfo_header_out, true); curl_setopt($ch, curlopt_useragent, $agent); curl_setopt($ch, curlopt_httpheader, $headers); $output = curl_exec($ch); $header = curl_getinfo($ch); curl_close($ch); // debugging junk echo nl2br($header["request_header"]); echo "<br/><br/>output:<br/><br/>$output";
the output script follows:
post /context?tab=login http/1.1
user-agent: user-agent: mozilla/5.0 (x11; u; linux i686; en-us; rv:1.9.2.13) gecko/20101206 ubuntu/10.10 (maverick) firefox/3.6.13
host: website
pragma: no-cache
referer: referer
accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
accept-language: en-us,en;q=0.5
accept-encoding: gzip,deflate
accept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.7
keep-alive: 115
connection: keep-alive
content-length: 46
content-type: application/x-www-form-urlencoded
loginid=username&password=password&login=login
output:
http/1.1 302
found
location:website/context?tab=p00697
content-language: en-us set-cookie:
jsessionid=0000tl8nl1hg2dbnv_penq-bbvr:-1;
path=/ set-cookie:
jsessionid=0000zue58y1txg3tt4xjb8exxw6:-1;
path=/ transfer-encoding: chunked
date: mon, 07 feb 2011 19:18:20 gmt
server: websphere application
server/6.1 expires: thu, 01 dec 1994
16:00:00 gmt cache-control:
no-cache="set-cookie,
set-cookie2"
based on i've posted, there obvious i'm missing? should try next? requests semantically same; i'm not sure doing incorrectly.
the 1 thing stands out following line of code:
$cookie = tempnam("/tmp", "curlcookie");
now if fails create file tempnam
return false, meaning following lines of code:
curl_setopt($ch, curlopt_cookiejar, $cookie); curl_setopt($ch, curlopt_cookiefile, $cookie);
are not being set @ all, should keep cookie file within same directory executing script.
the next thing is:
$fields = $_post; foreach($fields $key=>$value) { $fields_string .= "$key=$value&"; } $fields_string = substr($fields_string, 0, strlen($fields_string) - 1);
you not need curlopt_postfields
accepts array should able do:
curl_setopt($ch, curlopt_postfields, $_post);
this make sure entities correctly parsed.
i think can remove ini_set
that's native functions such file_get_contents
, fopen
streams etc, double check line:
ini_set("user_agent", $agent);
also check see if there cookie set main page, such index.php site may block requests sources have come directly login page data.
Comments
Post a Comment