The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2012, 08:43:33 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Problems with form sending (curl & multipart)  (Read 4182 times)
Boss
n00b
*
Offline Offline

Posts: 7


View Profile
« on: June 01, 2007, 05:01:35 PM »

In my last post you've said
"Problems with the code, issues with elements of it that you want to go over... language discussion, understanding more of the concepts behind it? This is the place."

So, now I have a problem with code. I want to send a message on tagworld with curl, but there's a problem. Here's my code (I can login and everything else):

Code:
//Message form
$msg['__EVENTTARGET'] = 'ctl00$ctl00$phBodyRight$phBodyRight$btnSend';
$msg['__EVENTARGUMENT'] = '';
$msg['__VIEWSTATE'] = $viewstate;
$msg['chooseuserradio'] = '1';
$msg['ctl00$ctl00$phBodyRight$phBodyRight$lcSendTo'] = $to;
$msg['ctl00$ctl00$phBodyRight$phBodyRight$tb_bmSubject'] = $subject;
$msg['ctl00$ctl00$phBodyRight$phBodyRight$RTE1'] = $message;
if(!empty($captcha)) {
$msg['ctl00$ctl00$phBodyRight$phBodyRight$tbUserCode'] = $captcha;
}

//Send message
curl_setopt($ch, CURLOPT_URL, "http://www.tagworld.com/$uname/SendMessage.aspx?recipient=$to");
curl_setopt($ch, CURLOPT_REFERER, "http://www.tagworld.com/$uname/SendMessage.aspx?recipient=$to");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/1.6");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $msg);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);

When I send the form above, it only sends a part of the form.
I have tried with posting to file on my server and do the print_r of $_POST, but here's what I got then:

Code:
POST:
Array
(
    [Content-Type:_multipart/form-data;_boundary] => ----------------------------69a8b1ab6d4d
------------------------------69a8b1ab6d4d
Content-Disposition: form-data; name=\"__EVENTTARGET\"

ctl00$ctl00$phBodyRight$phBodyRight$btnSend
------------------------------69a8b1ab6d4d
Content-Disposition: form-data; name=\"__EVENTARGUMENT\"


------------------------------69a8b1ab6d4d
Content-Disposition: form-data; name=\"__VIEWSTATE\"

/wEPDwUKMTY1NDA1MTM0MA8WCB4LUmVmZXJyZXJVcmwFImh0dHA6Ly93d3cudGFnd29ybGQuY29tL2d1em9saXphYy8eB2NhcHRjaGFlHgdzdHJDb2RlZR4IaWRPZkNvZGVmFgJmD2QWAmYPZBYCAgMPZBYKAgEPZBYEZg9kFgRmD2QWAgIBDxYEHgRocmVmBStodHRwOi8vd3d3LnRhZ3dvcmxkLmNvbS9qb25rdXNhbi9JbmJveC5hc3B4Hglpbm5lcmh0bWwFAygyKWQCAQ8WBB8EBThodHRwOi8vd3d3LnRhZ3dvcmxkLmNvbS8tL1dvcmxkL1RyYWNraW5nLmFzcHg/YWZmX2lkPTkwNR4HVmlzaWJsZWhkAgIPDxYCHwZoZBYCAgEPFgQfBAUraHR0cDovL3d3dy50YWd3b3JsZC5jb20vam9ua3VzYW4vSW5ib3guYXNweB8FBQMoMilkAgIPZBYCAgIPZBYCZg8WAh4LXyFJdGVtQ291bnQCCWQCAw9kFgICAg9kFgJmDxYCHwcCAhYEAgEPZBYCZg9kFgJmDxYCHwcCBmQCAg9kFgJmD2QWAmYPFgIfB2ZkAggPZBYCZg9kFgQCBw8QZBAVARBTZWxlY3QgYSBsaXN0Li4uFQEBMBQrAwFnZGQCCw8PFgIfBmhkZAIKD2QWAgIGD2QWAmYPFgIfBmhkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBS5jdGwwMCRjdGwwMCRjdGwwMSRtZW51SGVhZGVyJGxvZ2luU3RhdHVzJGN0bDAxBS5jdGwwMCRjdGwwMCRjdGwwMSRtZW51SGVhZGVyJGxvZ2luU3RhdHVzJGN0bDAzXMKRiT4tr/RFCDc/1sdiyASCEeo=
------------------------------69a8b1ab6d4d
Content-Disposition: form-data; name=\"chooseuserradio\"

1
------------------------------69a8b1ab6d4d
Content-Disposition: form-data; name=\"ctl00$ctl00$phBodyRight$phBodyRight$lcSendTo\"

sendToUsername
------------------------------69a8b1ab6d4d
Content-Disposition: form-data; name=\"ctl00$ctl00$phBodyRight$phBodyRight$tb_bmSubject\"

PHP subject
------------------------------69a8b1ab6d4d
Content-Disposition: form-data; name=\"ctl00$ctl00$phBod
)

Like you see, it only sends a part of the form and when it gets to the message part, it stops. Does anyone know where's the problem?
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #1 on: June 04, 2007, 09:34:16 AM »

Hey Boss -

There was just a thread about this and crypt posted a reference to an article on posting a MP form with curl, it is all here:

http://www.perkiset.org/forum/php/multipartformdata_post_with_curl-t217.0.html

hope that helps
/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Boss
n00b
*
Offline Offline

Posts: 7


View Profile
« Reply #2 on: June 04, 2007, 11:46:53 AM »

Perkiset, thanks for your reply, but I don't have that kind of problem. I can send multipart form, but curl won't send the whole form, just a part of it.

I tried with fsockopen, and it sends the whole form. However, I haven't been using fsockopen for logging into websites, so it would be nice if you could write an example of storing cookies with fsockopen. What I mean, I need to login with fsockopen and then go with this cookie to another page in my account. How to store cookies and use them with fsockopen?
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #3 on: June 04, 2007, 11:55:25 AM »

Hey Jefe -

you don't "store" cookies locally with PHP - you simply receive and send them in the header. Storage of cookies is either automatic (if you send via the header) to a surfer, or it is manual using a library in JS like the "efficient local storage of cookies" code in the JS code repository here at the cache.

When using either fsockopen or simply fopen, you need to create the header manually and send it in front of your content. Here is the header creation PHP from the webrequest class in the repository:

Code:
if ($this->_method == 'GET')
{
$header = "GET $finalURL HTTP/1.1\r\n";
$header .= "Host: {$this->Host}\r\n";
$header .= "User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/417.9 (KHTML, like Gecko) Safari/417.8\r\n";
$header .= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n";
$header .= "Accept-Language: en-us,en;q=0.5\r\n";
$header .= "Accept-Encoding: \r\n";
$header .= "Accept-Charset: ISO-8859-1,utf-8:q=0.7,*;q=0.7\r\n";
if ($cookieStr) { $header .= "Cookie: $cookieStr\r\n"; }
$header .= "Connection: close\r\n\r\n";
$header .= "Content-Type: text/html\r\n";
$header .= "Content-Length: $requestLen\r\n";
} else {
$header = "POST $finalURL HTTP/1.1\r\n";
$header .= "Host: {$this->Host}\r\n";
$header .= "User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/417.9 (KHTML, like Gecko) Safari/417.8\r\n";
$header .= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n";
$header .= "Accept-Language: en-us,en;q=0.5\r\n";
$header .= "Accept-Encoding: \r\n";
$header .= "Accept-Charset: ISO-8859-1,utf-8:q=0.7,*;q=0.7\r\n";
if ($cookieStr) { $header .= "Cookie: $cookieStr\r\n"; }
$header .= "Connection: close\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: $requestLen\r\n\r\n";
$header .= "$postStr\r\n";
}

the variable $cookieStr was created earlier in a little code snippet like this:
Code:
$cookieStr = '';
$start = true;
foreach($this->cookieList as $name=>$value)
{
if (!$start) { $cookieStr .= '; '; }
$cookieStr .= "$name=$value";
$start = false;
}
... the array is simply an associative array that holds onto string values. I ensure that they are encoded (if there are dicey characters) in the "set" portion of the class. As I mentioned, the entire class is available in the repository.

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Boss
n00b
*
Offline Offline

Posts: 7


View Profile
« Reply #4 on: June 04, 2007, 01:33:34 PM »

Thanks for that. It's really helpful.
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #5 on: June 04, 2007, 01:51:13 PM »

No worries mate - spamho!  Wink

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
crypt
Rookie
**
Offline Offline

Posts: 12


View Profile
« Reply #6 on: June 05, 2007, 05:22:44 PM »

I'm here to save the day!!  Applause  Praise  ROFLMAO <- /loves the smilies

Ok first, check the response status... (after curl_exec())
Code:
$info = curl_getinfo($ch);
echo $info->http_code
...should be 100, and that's due to an annoying default header in curl.

add this to your options to work around it  Wink
Code:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

  <-still loves the smilies 
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #7 on: June 05, 2007, 05:23:50 PM »

Yo dude... been a while! Nice to see you back!
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
crypt
Rookie
**
Offline Offline

Posts: 12


View Profile
« Reply #8 on: June 05, 2007, 05:31:24 PM »

Been busy making money  Wink ...need a bit of help myself now, so thought i'd share a bit of knowledge - and this problem was a major "wtf" for me when I ran into it... I'll try to stop by more often - promise  Angel
Logged

No links in signatures please
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!