【问题标题】:PHP GET and POST data with fsockopen使用 fsockopen 的 PHP GET 和 POST 数据
【发布时间】:2011-05-07 07:43:54
【问题描述】:

如何使用 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);
}
}

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

【问题讨论】:

    标签: php sockets variables post fsockopen


    【解决方案1】:

    别忘了fflush()

    【讨论】:

    • 它通过流刷新所有缓冲数据。
    【解决方案2】:

    您在同一连接中执行 GET 和 POST,这对于您已指定并通过连接重新确认的 HTTP/1.0 无效:关闭。注释掉你的 get 部分,然后发帖。

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

    【讨论】:

    • 我需要先获取 $view_state 值,否则它将为空。当我重新建立一个新连接时,$view_state 会有所不同,所以这无济于事。
    • 然后在 GET 之后,关闭套接字,并为 POST 打开另一个连接。在您的 get 缓冲区清空后,连接应该会被断开,并且您的帖子会被忽略。
    • 哎呀,重新阅读你的陈述。好吧,如果您必须拥有相同的连接,则需要切换到 http 1.1,并通过 Connection: Keep-Alive 而不是 connection: close 使用持久连接
    • 好的,那我先做 get 请求,用 fflush 刷新,然后做 post?
    【解决方案3】:

    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/

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 2011-01-22
      • 2016-10-18
      • 2012-11-29
      • 2011-09-15
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多