gpt4 book ai didi

php - 使用 fsockopen 获取页面

转载 作者:行者123 更新时间:2023-12-03 12:00:35 24 4
gpt4 key购买 nike

我正在尝试访问 http://www.example.com:4380/apid/request?method=getXMLTable&name=1&ui=UI&id=12345 .它应该以 XML 形式返回输出。
这是代码:

<?

$host = "www.example.com";
$path = "/apid/request?method=getXMLTable&name=1&ui=UI&id=12345";
$port = 4380;

$fp = fsockopen($host, $port, $errno, $errstr, 30);
$buffer ="";
if (!$fp) {
echo "ERR!!<br>";
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET ".$path." HTTP/1.1\r\n";
$out .= "Host: ".$host."\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);

while (!feof($fp)) {
$buffer .= fgets($fp, 1024);
}
fclose($fp);
}
?>
无论如何,我得到的不是 XML 输出,而是这个响应:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Last-Modified: Wed, 02 Apr 2014 16:16:43 GMT
Cache-Control: no-store
Cache-Control: no-cache
Cache-Control: must-revalidate
Cache-Control: pre-check=0
Cache-Control: post-check=0
Cache-Control: max-age=0
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: text/xml
Transfer-Encoding: chunked
Vary: Accept-Encoding
Date: Wed, 02 Apr 2014 16:16:43 GMT
Connection: close 2000 0
我可以访问像 'www.example.com/path/to/file' 这样的页面使用提供的代码没有任何问题。我想我对端口(不是标准的 http )或请求有任何错误。每条线索都会非常受欢迎。
编辑:
can't使用任何 http 模块!在这种情况下,套接字是必须的!
我尝试过使用下面的代码,但在 $body 中什么也没有:
list($header, $body) = explode("\n\n", $buffer, 2);
echo http_chunked_decode($body);

最佳答案

您将收到 Transfer-Encoding: chunked回复。您需要使用 http_chunked_decode解码它(你可以找到一个 PHP version here ,如果你没有 pecl_http )。

list($header, $body) = explode("\n\n", $buffer, 2);
echo http_chunked_decode($body);

但正如其他评论者所说,你真的应该使用 file_get_contents如果你有 allow_url_fopen启用或 curl除此以外。他们为您处理分 block 编码。

关于php - 使用 fsockopen 获取页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22817503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com