【问题标题】:Using fsockopen to POST to a GET query URL使用 fsockopen POST 到 GET 查询 URL
【发布时间】:2012-02-04 16:56:18
【问题描述】:

这个使用 fsockopen 的原始 HTTP 调用有什么问题?

POST /api/?action=report HTTP/1.1
Host: www.webhosting-performance.com
Content-Type: application/x-www-form-urlencoded
Conent-Length: 9
Connection: close

test=test

远程脚本说 var_dump($_POST) 为空。

我的 php 代码的 sn-p 如下所示:

if (ini_get('allow_url_fopen') > 0) {

  $parts = parse_url($url);
  $fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30);

  if (!$fp) throw new Exception("Problem with $url, $errstr");

  $post_string = $post_fields ? http_build_query($post_fields) : '';

  $out = ($post_string ? "POST " : "GET ") . $parts['path'] . ((isset($parts['query'])) ? "?" . $parts['query'] : false) ." HTTP/1.1\r\n"
       . "Host: ". $parts['host'] ."\r\n"
       . ($post_string ? "Content-Type: application/x-www-form-urlencoded\r\n" : false)
       . "Conent-Length: ". strlen($post_string) ."\r\n"
       . "Connection: close\r\n"
       . "\r\n" . $post_string;

  fwrite($fp, $out);

  // ...

} else {
  // use curl
}

【问题讨论】:

    标签: php post get fsockopen


    【解决方案1】:

    你基本上只是一个错字:

    Conent-Length: 9
    

    Content-Length: 中查看缺少的t

    真正的问题是(不要诋毁你自己构建东西的努力),你为什么不使用cURLPEARs HTTP Request 类?

    【讨论】:

    • 多年的经验,以及如此烦人的小错字。我坐了几个小时试图解决这个问题。错误地一直复制那个错字。非常感谢您让我免于再做一个小时;)干杯
    • 至于你的其他问题。我试图尽可能地做到本地化,以防客户平台不支持扩展。至于这个功能,如果禁用了allow_url_fopen,它支持curl。我也可能会添加 PEAR。
    【解决方案2】:

    你为什么不用PEAR::Http_Request2

    【讨论】:

      猜你喜欢
      • 2011-05-07
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多