【问题标题】:PHP Post data with FsockopenPHP 使用 Fsockopen 发布数据
【发布时间】:2011-01-22 23:20:44
【问题描述】:

我正在尝试使用 fsockopen 发布数据,然后返回结果。 这是我当前的代码:

<?php
$data="stuff=hoorah\r\n";
$data=urlencode($data);

$fp = fsockopen("www.website.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "POST /script.php HTTP/1.0\r\n";
    $out .= "Host: www.webste.com\r\n";
    $out .= 'Content-Type: application/x-www-form-urlencoded\r\n';
    $out .= 'Content-Length: ' . strlen($data) . '\r\n\r\n';
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?> 

它应该是回显页面,它正在回显页面,但是这里是script.php的脚本

<?php
echo "<br><br>";    
$raw_data = $GLOBALS['HTTP_RAW_POST_DATA'];  
 parse_str( $raw_data, $_POST );

//test 1
var_dump($raw_data);
echo "<br><br>":
//test 2
print_r( $_POST );  
?>

结果是:

HTTP/1.1 200 OK 日期:2010 年 3 月 2 日,星期二 格林威治标准时间 22:40:46 服务器:Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.2.6 内容长度:31 连接:关闭 内容类型:文本/html;字符集=UTF-8 string(0) "" 数组 ( )

我有什么问题?为什么变量不发布其数据?

【问题讨论】:

  • 我不能使用 curl 因为这将是开源的
  • “Curl 是免费且开放的软件” Curl 在 MIT/X 许可下获得许可。
  • 你不能指望每个人(甚至是共享主机的人)都想要并获得 curl
  • 我可能错了,但每个标题字段之后不应该只有一个 \r\n 吗?当然最后一个除外……
  • 您是否启用了always_populate_raw_post_dataphp.net/manual/en/…

标签: php sockets fsockopen


【解决方案1】:

$data 绝不会被写入套接字。您想添加如下内容:

$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
fwrite($fp, $data);

【讨论】:

  • 我这样做了,但仍然没有雪茄。
【解决方案2】:

试试这个

$out .= 'Content-Length: ' . strlen($data) . '\r\n';
$out .= "Connection: Close\r\n\r\n";
$out .= $data;

【讨论】:

  • 尝试在您的服务器上安装数据包嗅探器并查看实际发送到服务器的标头。它们与您在 php 中发送的相同吗?
【解决方案3】:

是否使用cURL 和选项?

【讨论】:

    【解决方案4】:

    试试这个:

    <?php
    $data="stuff=hoorah\r\n";
    $data=urlencode($data);
    
    $fp = fsockopen("www.website.com", 80, $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        $out = "POST /script.php HTTP/1.0\r\n";
        $out .= "Host: www.webste.com\r\n";
        $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $out .= 'Content-Length: ' . strlen($data) . "\r\n\r\n";
        $out .= "Connection: Close\r\n\r\n";
        fwrite($fp, $out);
        fwrite($fp, $data);
        while (!feof($fp)) {
            echo fgets($fp, 128);
        }
        fclose($fp);
    }
    ?> 
    

    \n 等某些字符转义在单引号中不起作用。

    【讨论】:

      【解决方案5】:

      您的代码中有许多小错误。这是一个经过测试并且可以工作的 sn-p。

      <?php
      
      $fp = fsockopen('example.com', 80);
      
      $vars = array(
          'hello' => 'world'
      );
      $content = http_build_query($vars);
      
      fwrite($fp, "POST /reposter.php HTTP/1.1\r\n");
      fwrite($fp, "Host: example.com\r\n");
      fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
      fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
      fwrite($fp, "Connection: close\r\n");
      fwrite($fp, "\r\n");
      
      fwrite($fp, $content);
      
      header('Content-type: text/plain');
      while (!feof($fp)) {
          echo fgets($fp, 1024);
      }
      

      然后在 example.com/reposter.php 上放这个

      <?php print_r($_POST);
      

      运行时你应该得到类似的输出

      HTTP/1.1 200 OK
      Date: Wed, 05 Jan 2011 21:24:07 GMT
      Server: Apache
      X-Powered-By: PHP/5.2.9
      Vary: Host
      Content-Type: text/html
      Connection: close
      
      1f
      Array
      (
          [hello] => world
      )
      0
      

      【讨论】:

      • 为什么最后会显示“1f”和“0”?我也一样,但不太确定。
      • 它可以工作,但你必须将 fsockopen 更改为 fsockopen('www.example.com', 80);并在 fwrite 到 fwrite($fp, "Host: www.example.com\r\n");
      【解决方案6】:

      不错的 Tamlyn,效果很好!

      对于那些还需要与 url 一起发送 get vars 的人,

      //change this:
      
      fwrite($fp, "POST /reposter.php HTTP/1.1\r\n");
      
      //to:
      
      $query = 'a=1&b=2';
      fwrite($fp, "POST /reposter.php?".$query." HTTP/1.1\r\n");
      

      【讨论】:

        【解决方案7】:

        在reposter.php中试试这个

        $raw_data = $GLOBALS['HTTP_RAW_POST_DATA']; 
        parse_str( $raw_data, $_POST );
        
        print_r( $_POST );
        

        因为,数据不在$_POST[] 变量中,而是在$GLOBALS['HTTP_RAW_POST_DATA'] 变量中。

        【讨论】:

          【解决方案8】:

          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/

          【讨论】:

            【解决方案9】:

            抱歉刷新,但对于仍然遇到此类问题的人,将 HTTP/1.0 更改为 HTTP/1.1 即可。

            【讨论】:

              【解决方案10】:

              您可以使用这种技术,它有助于调用任意数量的页面,所有页面将同时独立运行,而无需等待每个页面响应异步。

              cornjobpage.php //主页面

                  <?php
              
              post_async("http://localhost/projectname/testpage.php", "Keywordname=testValue");
              //post_async("http://localhost/projectname/testpage.php", "Keywordname=testValue2");
              //post_async("http://localhost/projectname/otherpage.php", "Keywordname=anyValue");
              //call as many as pages you like all pages will run at once independently without waiting for each page response as asynchronous.
                          ?>
                          <?php
              
                          /*
                           * Executes a PHP page asynchronously so the current page does not have to wait for it to     finish running.
                           *  
                           */
                          function post_async($url,$params)
                          {
              
                              $post_string = $params;
              
                              $parts=parse_url($url);
              
                              $fp = fsockopen($parts['host'],
                                  isset($parts['port'])?$parts['port']:80,
                                  $errno, $errstr, 30);
              
                              $out = "POST ".$parts['path']."?$post_string"." HTTP/1.1\r\n";//you can use GET instead of POST if you like
                              $out.= "Host: ".$parts['host']."\r\n";
                              $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
                              $out.= "Content-Length: ".strlen($post_string)."\r\n";
                              $out.= "Connection: Close\r\n\r\n";
                              fwrite($fp, $out);
                              fclose($fp);
                          }
                          ?>
              

              testpage.php

                  <?
                  echo $_REQUEST["Keywordname"];//case1 Output > testValue
                  ?>
              

              PS:如果您想将 url 参数作为循环发送,请按照以下答案操作:https://stackoverflow.com/a/41225209/6295712

              【讨论】:

                猜你喜欢
                • 2016-01-18
                • 2014-02-06
                • 2012-04-03
                • 1970-01-01
                • 2011-11-15
                • 2011-05-07
                • 1970-01-01
                • 2016-03-08
                • 2013-07-30
                相关资源
                最近更新 更多