使用fsockopen打开php页面时,如何获取输出? [英] How to get output, when using fsockopen to open a php page?
问题描述
当我使用fsockopen打开php页面时,代码可以正常工作,但是还有其他一些问题.例如:如果我在a.php中打开b.php,则"echo"在b.php中将不起作用,也不会出现错误消息(这2件事在普通页面上都可以正常工作).这使得调试非常困难.如何在b页中获取输出?
when I use fsockopen to open a php page, the code works fine, but there are some other problems. For Example: if I open b.php in a.php, "echo" won't work in b.php, error message neither(these 2 things works fine on common page). This makes debug very difficult. How to get output in page b?
非常感谢!这是我的代码.我使用main.php调用main_single_block.php.PS:除上面提到的两件事外,其他所有东西都工作正常.
Thanks a lot! here is my code. I use main.php to call main_single_block.php.PS: all things work fine except the 2 things I mentiond above.
main.php:
$template_url_arr_s = serialize($template_url_arr);
$fp = fsockopen($sochost, intval($socportno), $errno, $errstr, intval($soctimeout));
if (!$fp) {
echo "$errstr ($errno) ,open sock erro.<br/>\n";
}
$typename= urlencode($typename);//do url encode (if not, ' 'can not be handled right)
$template_url_arr_s= urlencode($template_url_arr_s);
*$out = "GET /main/main_single_block.php?typename=" . $typename . "&templateurlarr=" . $template_url_arr_s . "\r\n";*
fputs($fp, $out);
fclose($fp);
推荐答案
以下是基本结构:
template_url_arr_s = serialize($template_url_arr);
$fp = fsockopen($sochost, intval($socportno), $errno, $errstr, intval($soctimeout));
if (!$fp) {
echo "$errstr ($errno) ,open sock erro.<br/>\n";
}
$typename= urlencode($typename);//do url encode (if not, ' 'can not be handled right)
$template_url_arr_s= urlencode($template_url_arr_s);
$out = "GET /main/main_single_block.php?typename=" . $typename . "&templateurlarr=" . $template_url_arr_s . " HTTP/1.1\r\nHost: $sochost\r\nConnection: close\r\n\r\n";
fputs($fp, $out);
// First read until the end of the response header, look for blank line
while ($line = fgets($fp)) {
$line = trim($line);
if ($line == "") {
break;
}
}
$output = '';
// Read the body of the response
while ($line = fgets($fp)) {
$output .= $line;
}
fclose($fp);
我在GET
行的末尾添加了HTTP/1.1
参数,必需的Host:
标头和Connection: close
标头,因此我无需处理解析Content-Length:
标头响应.
I've added the HTTP/1.1
parameter to the end of the GET
line, the required Host:
header, and a Connection: close
header so I don't need to deal with parsing the Content-Length:
header of the response.
一个真正的应用程序应该解析响应头,我上面的代码只是跳过它们.标头以空行结束,然后将其余输出收集到变量中.
A real application should parse the response headers, my code above just skips over them. The header is terminated by a blank line, then it collects the rest of the output into a variable.
这篇关于使用fsockopen打开php页面时,如何获取输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!