Digital Human
Digital Human

Reputation: 1637

PHP GET and POST data with fsockopen

How to get, and post data through the same socket with PHP? I have this code:

$fp = fsockopen("ssl://ovi.rdw.nl", 443, $errno, $errstr, 30);
if(!$fp){
    echo $errstr;
}else{
$post_data = 'ctl00$cntMaincol$btnZoeken=Zoeken&ctl00$cntMaincol$txtKenteken=83FHVN';

$out = "GET /Default.aspx HTTP/1.0\r\n";
$out .= "Host: ovi.rdw.nl\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);

while(!feof($fp)){
    $data = fgets($fp);
    $view_state = getViewState($data);
    if($view_state != ""){
        echo $view_state."<br />";
        break;
    }
}

$post_data = "__VIEWSTATE={$view_state}&".$post_data;

$out = "POST /Default.aspx HTTP/1.0\r\n";
$out .= "Host: ovi.rdw.nl\r\n";
$out .= "Content-type: application/x-www-form-urlencoded\r\n";
$out .= "Content-length: " . strlen($post_data) . "\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
fwrite($fp, $post_data);
while(!feof($fp)){
    echo fgets($fp);
}
}

It get's the data right, but the posting it is not going ok. What do i mis?

Upvotes: 1

Views: 3846

Answers (3)

diyism
diyism

Reputation: 12935

Curl is too heavy in some case, to use post_to_host():

//GET:
$str_rtn=post_to_host($str_url_target, array(), $arr_cookie, $str_url_referer, $ref_arr_head, 0);

//POST:
$arr_params=array('para1'=>'...', 'para2'=>'...');
$str_rtn=post_to_host($str_url_target, $arr_params, $arr_cookie, $str_url_referer, $ref_arr_head);

//POST with file:
$arr_params=array('para1'=>'...', 'FILE:para2'=>'/tmp/test.jpg', 'para3'=>'...');
$str_rtn=post_to_host($str_url_target, $arr_params, $arr_cookie, $str_url_referer, $ref_arr_head, 2);

//raw POST:
$tmp=array_search('uri', @array_flip(stream_get_meta_data($GLOBALS[mt_rand()]=tmpfile())));
$arr_params=array('para1'=>'...', 'para2'=>'...');
file_put_contents($tmp, json_encode($arr_params));
$arr_params=array($tmp);
$str_rtn=post_to_host($str_url_target, $arr_params, $arr_cookie, $str_url_referer, $ref_arr_head, 3);

//get cookie and merge cookies:
$arr_new_cookie=get_cookies_from_heads($ref_arr_head)+$arr_old_cookie;//don't change the order

//get redirect url:
$str_url_redirect=get_from_heads($ref_arr_head, 'Location');

post to host php project location: http://code.google.com/p/post-to-host/

Upvotes: 0

superfro
superfro

Reputation: 3302

You're doing a GET and a POST in the same connection, This isn't valid for HTTP/1.0 which you have specified and re-assured via connection: close. Comment out your get portion and just do the post.

You can get data back with a post, so you don't need to do a get and a post. Or if you do need to do a get and a post, close the socket, then re-establish the socket again for the post.

Upvotes: 1

jay.lee
jay.lee

Reputation: 19837

don't forget to fflush()

Upvotes: 0

Related Questions