cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

File Upload & Post problem - libcurl + PHP

From: lakshmipriya prabhu <prabhu_priya_at_yahoo.com>
Date: Tue, 2 May 2006 10:35:16 -0700 (PDT)

Hi,
   
  I am using libcurl + PHP to upload a file and also post some parameters to a website after successful login.
The file to be uploaded and the parameters to be posted is set in an array and the array is posted to the website.
Cookie- handling is also taken care of in the code
  libcurl version used : curl 7.15.3 (i586-pc-mingw32msvc) libcurl/7.15.3 OpenSSL/0.9.7c zlib/1.2.2
php version used:php-5.1.2
  
Problem : Upload & post to the website is not happening and i am getting directed to the error page with trace summary as below
   Expect: 100-continue
 Content-Type: multipart/form-data; boundary=----------------------------0b038ba78c65
   < HTTP/1.1 100 Continue
 < HTTP/1.1 200 OK
   
  Could anyone tell why this problem occurs?
Please find my code and output trace below.
  Thanks,
LP
  CODE:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?php
   $cookie_file_path = "C:/test/test_cookie.txt"; // Cookie File path
 $welcomefile ="C:/test/test_welcome.html"; //welcome result
 $resultfile = "C:/test/test_two.html"; //step two result
 $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
 $reffer = "http://pp.qq.rr/"; //referer URL
 $LOGINURL = "https://pp.qq.rr/yy/login.aspx?";
 $STEPTWOURL="https://pp.qq.rr/zz/upload.aspx?hy=name%20mert&amp;ph=(555)%5666-5619&amp;pe=abc_xyz@nnn.com&amp;ft=UUU5&amp;wt=PPFILE";
 
  //-- Before posting parameters to the login page , parsing and getting the viewstate variable
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$LOGINURL);
  curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
  $result = curl_exec ($ch);
    preg_match_all("/name=\"__VIEWSTATE\" value=\"(.*?)\"/", $result, $arr_hash_viewstateId);
  $hash_viewstateId = $arr_hash_viewstateId[1][0];
  $hash_viewstateId = urlencode($hash_viewstateId);
    curl_close ($ch);
    $event_target=urlencode('_gt56:loginLink');
  $event_argument=urlencode('"');
  $POSTFIELDS='__EVENTTARGET='.$event_target.'&__EVENTARGUMENT='.$event_argument.'&__VIEWSTATE='.$hash_viewstateId.'&Username=myid&Password=password&Login=In%20In';
   
          echo "\n \n ## POSTFIELDS ON WELCOME : $POSTFIELDS \n \n";
 
  //--If "System Unavailable" page is result , do nothing
  if (stristr($result, "System Unavailable"))
  {
   return;
  } //if login fail
  ////////////////////////////////////// LOGIN ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
  //--Posting parameters to the login page with the viewstate
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$LOGINURL);
  curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_REFERER, $reffer);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
  
  // output to file
  $fp = fopen ($welcomefile , "w");
  curl_setopt ($ch , CURLOPT_FILE, $fp);
    // execute(form login)
  $result =curl_exec ($ch );
    // close resources
  curl_close ($ch );
  fclose ($fp);
   ////////////////////////////////////// LOGIN /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 ////////////////////////////////////// STEP TWO///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //-- Before posting parameters to the file upload page , parsing and getting the viewstate variable
 
 //replace '&amp' in the URL with '&'
 $STEPTWOURL=str_replace('&amp;','&',$STEPTWOURL);
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$STEPTWOURL);
 curl_setopt($ch, CURLOPT_USERAGENT, $agent);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
 $result = curl_exec ($ch);
 
 preg_match_all("/name=\"__VIEWSTATE\" value=\"(.*?)\"/", $result, $arr_hash_viewstateId);
 $hash_viewstateId = $arr_hash_viewstateId[1][0];
 $hash_viewstateId = urlencode($hash_viewstateId);
 
 curl_close ($ch);
   //-- Posting parameters to the file upload page with the viewstate ("multipart/form-data format")
 
 $submitbutton=urlencode('File Submit >>');
 $uploadfile = "sampletest.txt";
 $postData = array();
 $postData[ '__EVENTTARGET' ] = "''";
 $postData[ '__EVENTARGUMENT' ] = "''";
 $postData[ '__VIEWSTATE' ] = $hash_viewstateId;
 $postData[ 'inFile' ] = "@sampletest.txt";
 $postData[ 'btnSubmit' ] = $submitbutton;
 
 //$postData = array();
   echo " \n \n ::::::::::::::::::::::: POSTDATA STEP TWO : $postData \n";
 foreach($postData as $key => $value) {
    print "\n $key: $value<br>";
 }
   //--If "System Unavailable" page is result , do nothing
 if (stristr($result, "System Unavailable"))
 {
  return;
 } //if login fail
    
 //--Posting parameters to the file upload page with the viewstate
   $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $STEPTWOURL);
 curl_setopt($ch, CURLOPT_USERAGENT, $agent);
 curl_setopt($ch, CURLOPT_POST, 1 );
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_VERBOSE, 1);
 curl_setopt($ch, CURLOPT_HEADER, 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($ch, CURLOPT_REFERER, $reffer);
 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
 
 // output to file
 $fp = fopen ($resultfile, "w");
 curl_setopt ($ch , CURLOPT_FILE, $fp);
   // execute(form login)
 $result =curl_exec ($ch );
   // close resources
 curl_close ($ch );
 fclose ($fp);
////////////////////////////////////// STEP TWO ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// LOGIN ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
  --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  
OUTPUT TRACE :
==============================================================================================
   ## POSTFIELDS ON WELCOME : __EVENTTARGET=_gt56%3loginLink&__EVENTARGUMENT=%22&__VIEWSTATE=dDwtMjA2MDg5NDYzMzt0PDtsPG
k8MT47aTw0Pjs%2BO2w8dDxwPGw8aW5uZXJodG1sOz47bDxTaWduIEluOz4%2BOzs%2BO3Q8O2w8aTwyPjtpPDY%2BO2k8MTA%2BOz47bDx0PDtsPGk8MT47aT
wzPjs%2BO2w8dDxwPHA8bDxDb21tb24uRGVmYXVsdEhlYWRlci5BZnRlclNpZ25JblJldHVyblRvOz47bDxNaWNoaWdhbi5VbmVtcGxveW1lbnQuV2ViLkIyRy
5Db21tb24uRGVmYXVsdEhlYWRlcitSZXR1cm5Ub1BhZ2UsIE1pY2hpZ2FuLlVuZW1wbG95bWVudC5XZWIuQjJH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8Mz47aTw3PjtpPDk%2BO2k8MTE%2BO2k8MTM%2BO2k8MTU%2BOz47bDx0PHA8cDxsPE5
hdmlnYXRlVXJsOz47bDxodHRwOi8vbWljaGlnYW4uZ292Oz4%2BOz47Oz47dDxwPHA8bDxOYXZpZ2F0ZVVybDs%2BO2w8aHR0cDovL3d3dy5taWNoaWdhbi5nb
3Yvc29tLzAsMTYwNyw3LTE5Mi0tLS1BLDAwLmh0bWw7Pj47Pjs7Pjt0PHA8cDxsPE5hdmlnYXRlVXJsOz47bDxodHRwOi8vd3d3Lm1pY2hpZ2FuLmdvdi9zb20
vMCwxNjA3LDctMTkyLTI2OTE0LTIwODgtLSwwMC5odG1sOz4%2BOz47Oz47dDxwPHA8bDxOYXZpZ2F0ZVVybDs%2BO2w8aHR0cDovL3d3dy5taWNoaWdhbi5nb
3Yvc29tLzAsMTYwNyw3LTE5Mi0yNjkxNS0yMDg5LS0sMDAuaHRtbDs%2BPjs%2BOzs%2BO3Q8cDxwPGw8TmF2aWdhdGVVcmw7PjtsPGh0dHA6Ly93d3cubWlja
GlnYW4uZ292L3NvbS8wLDE2MDcsNy0xOTItMjY5MTMtMjA5MC0tLDAwLmh0bWw7Pj47Pjs7Pjt0PHA8cDxsPE5hdmlnYXRlVXJsOz47bDxodHRwOi8vd3d3Lm1
pY2hpZ2FuLmdvdi9zb20vMCwxNjA3LDctMTkyLTI2OTE2LTIzMDEtLSwwMC5odG1sOz4%2BOz47Oz47dDxwPHA8bDxUZXh0Oz47bDxEZXBhcnRtZW50IG9mIEx
hYm9yICYgRWNvbm9taWMgR3Jvd3RoOz4%2BOz47Oz47Pj47Pj47Pj47Pj47Pth%2F5Bu2oGJ0f4L1cKElMk7tqFDd<br>
 inFile: @sampletest.txt<br>
 btnSubmit: File+Submit%3E%3E<br>* About to connect() to pp.qq.rr port 443
* Trying 136.181.145.40... * connected
* Connected to pp.qq.rr (134.141.143.40) port 443
* SSL connection using RC4-MD5
* Server certificate:
* subject: /C=US/ST=TTT/L=Lansing/O=YYY/OU=PP.QQ.RR/OU=Terms of use at www.verisig
n.com/rpa (c)00/CN=PP.QQ.RR
* start date: 2005-10-28 00:00:00 GMT
* expire date: 2006-10-28 23:59:59 GMT
* issuer: /C=US/O=RSA Data Security, Inc./OU=Secure Server Certification Authority
* SSL certificate verify result: error number 1 (20), continuing anyway.
> POST zz/upload.aspx?hy=name%20mert&amp;ph=(555)%5666-5619&amp;pe=abc_xyz_at_nnn.com&amp;ft=UUU5&amp;wt=PPFILE HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
Host: PP.QQ.RR
Accept: */*
Referer: http://PP.QQ.RR/
Cookie: .ASPXAUTH=8469F9AC1904321959106FF7C5748E8799667303E205781ED1168E872907D082FB9902DA1FB1EF778CEB5F
42DDA47C22D3CC137A46AEE5AB46D4B4912DAF3746966C3F61BAC4A8B44770857C45BC5134E38EEF9F257561
D3B0C363AA602A22071F053B2FAD896A0651; ASP.NET_SessionId=r3gouertertertgmfe3bc4445
Content-Length: 3843
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------0b038ba78c65
  < HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Date: Tue, 02 May 2006 17:04:36 GMT
< Server: Microsoft-IIS/6.0
< X-Powered-By: ASP.NET
< X-AspNet-Version: 1.1.4322
< Cache-Control: no-cache
< Pragma: no-cache
< Expires: -1
< Content-Type: text/html; charset=utf-8
< Content-Length: 9276
* Curl_xxx_rcvs returned -1, block = TRUE
* Curl_xxx_rcvs returned -1, block = TRUE
* Curl_xxx_rcvs returned -1, block = TRUE
* Connection #0 to host PP.QQ.RR left intact
* Closing connection #0
  ==============================================================================================

                
---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2006-05-02