如何在PHP中作为客户端与CGI服务器通信


How to communicate as client with CGI server in PHP?

使用PHP,我需要向CGI服务器发送请求,然后接收相应的响应。到目前为止,我所做的似乎是正确的,直到我试图读取响应的那一刻:

<?php
$request_body = '-----------------9022632e1130lc4'r'n';
$request_body .= 'Content-Disposition: form-data; name="rutSender"'r'n';
...
$request_body .= '</EnvioDTE>'r'n';
$request_body .= ''r'n';
$request_body .= '-----------------9022632e1130lc4--'r'n';
$request = 'POST /cgi_dte/UPL/DTEUpload HTTP/1.0'r'n';
$request .= 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/ms-excel, application/msword, */*'r'n';
...
$request .= ''r'n';
$request .= $request_body;
$hostName = 'host.name.example';
$host = gethostbyname($hostName);
$port = 443;
$fp = fsockopen($hostName, $port);
fwrite($fp, $request);

下面我尝试读取响应,但我收到"致命错误:最大执行时间超过120秒":

while(!feof($fp)){
    echo fgets($fp, 1024);
}
?>

由于我是使用这种通信方法的新手,在我看来,在PHP世界中存在许多选择来尝试解决这个问题,如不同的套接字函数,扩展和库(不是说我没有尝试过4天)…我很困惑,需要方向。

请求后需要2行换行符,而不仅仅是您拥有的那一行。请尝试在请求末尾添加"'r'n"。

$request .= $request_body."'r'n";

作为旁注,您不应该为所有这些烦恼。