2012-01-07 60 views
2

使用fsockopen这个原始HTTP调用有什么问题?使用fsockopen POST到GET查询URL

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代码片段看起来是这样的:

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 
} 

回答

2

你基本上只是有一个错字:

Conent-Length: 9 

Content-Length:

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

+0

多年的经验,以及如此唠叨的小错字。我坐了几个小时试图解决这个问题。错误地不断复制该错字。非常感谢您为我节省了一小时的时间;)干杯 – tim 2012-01-07 03:15:58

+0

至于您的其他问题。我试图尽可能本地化,因此客户平台不支持incase扩展。至于这个函数,如果allow_url_fopen被禁用,它支持curl。我也可以加PEARs。 – tim 2012-01-07 03:45:17