PHP 使用 fsockopen 获取和发布数据

发布于 2024-10-01 12:56:44 字数 974 浏览 6 评论 0原文

如何使用 PHP 通过同一个套接字获取和发布数据?我有这样的代码:

$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);
}
}

它得到的数据是正确的,但发布它并不顺利。我错过了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情话墙 2024-10-08 12:56:44

您正在同一连接中执行 GET 和 POST,这对于您已通过连接指定并重新保证的 HTTP/1.0 无效:关闭。注释掉你的获取部分,然后就可以发帖了。

您可以通过 post 获取数据,因此不需要执行 get 和 post。或者,如果您确实需要执行 get 和 post,请关闭套接字,然后再次为 post 重新建立套接字。

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.

又爬满兰若 2024-10-08 12:56:44

不要忘记 fflush()

don't forget to fflush()

梦归所梦 2024-10-08 12:56:44

在某些情况下,Curl 太重,无法使用 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');

发布到托管 php 项目位置: http://code.google.com/p/post-to-host/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文