我有一个问题与基于笨的应用程序我建设的一部分。与fgets()超时
我必须连接到远程服务器,并有一个合理的一整套详细的约如何打开一个套接字,发送一些XML数据和接收响应第三方指令 - 在VB脚本。我已经将它翻译成下面的PHP代码。目前,根据我的日志,数据正在写入服务器(日志输出如下所示)。然而,代码中的fgets()部分似乎是超时的,没有任何错误或任何东西......使得调试非常困难。
从我读到的关于fsockopen()和fgets(),下面应该工作,但显然它不。问题在于没有错误消息等等,我在如何调试时遇到了一些问题。
我将不胜感激,如果任何人谁拥有一个备用分钟能超过它投了眼睛只是为了看看,如果我犯了一个明显的错误(或确认我在做什么是正确的这个问题)。
下面的代码(我不得不空出它的一些遗憾)。
public function makeBooking($orderId) {
// get entire XML file as a string
$xmlString = $this->getXmlString();
// replace non-alphanumeric characters
$xmlString = $this->replaceChars($xmlString);
// get length of xml string
$xmlStrCount = mb_strlen($xmlString) + 7;
// attempt to open socket
$fp = fsockopen('xxx.xx.xxx.xx', '81', $errno, $errstr, 10);
if (!$fp) {
// couldn't open socket
log_message('ERROR', "*** Couldn't open socket: $errno $errstr " . ' | line ' . __LINE__ . ' of ' . __FILE__);
} else {
log_message('DEBUG', "*** Socket open, connected to remote server ");
$lf = chr(13).chr(10);
// opened socket, prepare output
$output = '"POST xxxxxxxxxxxx.asp HTTP/1.0"' . $lf;
$output .= '"Accept: */*"' . $lf;
$output .= '"User-Agent: xxxxxxxxxxx_socket/1.0";' . $lf;
$output .= '"Content-type: application/x-www-form-urlencoded";' . $lf;
$output .= '"Content-length: ' . $xmlStrCount . '";' . $lf;
$output .= '""' . $lf;
$output .= '"xml_in='.$xmlString.'"';
// attempt to write output
$result = fwrite($fp, $output);
log_message('DEBUG', "*** Bytes written: $result " . ' | line ' . __LINE__ . ' of ' . __FILE__);
if($result) {
log_message('DEBUG', "*** Wrote output:$lf$lf$output$lf");
} else {
log_message('ERROR', "*** Failed to write output:$lf$lf$output$lf");
}
// read data from server until no more responses are sent
// while (!feof($fp)) {
if(!feof($fp)) {
log_message('DEBUG',"*** Requesting line 1 from remote server");
//$line = fgets($fp);
$line = fgets($fp);
if($line) {
log_message('DEBUG',"*** Retreived line 1: " . print_r($line,TRUE));
} else {
log_message('ERROR',"*** Could not retreive line 1");
}
}
fclose($fp);
log_message('DEBUG',"*** Closed connection to remote server");
}
}
日志。很显然,我已经解释了相关的结果。我截断了URL编码的XML数据:
DEBUG - 2012-09-21 14:50:01 --> *** Socket open, connected to remote server
DEBUG - 2012-09-21 14:50:01 --> *** Bytes written: 8801 | line 57
DEBUG - 2012-09-21 14:50:01 --> *** Wrote output:
"POST xxxxxxxxxxxx.asp HTTP/1.0"
"Accept: */*"
"User-Agent: xxxxxxxxxxxxx_socket/1.0";
"Content-type: application/x-www-form-urlencoded";
"Content-length: 8630";
""
"xml_in=%26lt%3B%3Fxml+version%3D%26quot%3B1.0%26quot%3B..."
DEBUG - 2012-09-21 14:50:01 --> *** Requesting line 1 from remote server
ERROR - 2012-09-21 14:51:01 --> *** Could not retreive line 1
DEBUG - 2012-09-21 14:51:01 --> *** Closed connection to remote server
任何有关在哪里寻找的想法将不胜感激。
-------------- --------------编辑
只是柜面任何人碰到这个线程绊倒了类似的问题,这里的我就是这样做到底,根据Robbie的建议:
public function createBooking($orderId) {
// http://php.net/manual/en/book.curl.php
// get entire XML file as a string
$xmlString = $this->getXmlString();
// replace non-alphanumeric characters - can't use htmlentities or htmlspecialchars
// because remote server uses different codes
$xmlString = $this->replaceChars($xmlString);
// get length of xml string
$xmlStrCount = mb_strlen($xmlString) + 7;
// prepare output
$lf = chr(13).chr(10);
$output = '"Accept: */*"' . $lf;
$output .= '"User-Agent: xxxxx_socket/1.0";' . $lf;
$output .= '"Content-type: application/x-www-form-urlencoded";' . $lf;
$output .= '"Content-length: ' . $xmlStrCount . '";' . $lf;
$output .= '""' . $lf;
$output .= '"xml_in='.$xmlString.'"';
// set curl options
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://xxxxx:xx/xxxxx.asp");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $output);
// Request the header information in the response.
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Now execute the query
$response = curl_exec ($ch);
if(!$response) {
log_message('ERROR', "*** Curl error: " . curl_error($ch) . ' | line ' . __LINE__ . ' of ' . __FILE__);
show_error(DEFAULT_ERR_MSG);
}
curl_close ($ch);
// Response is the data.
$aLines = explode("\n", $response); // Splits the resposne into an array so you can process individual lines.
// etc
print_r($aLines);
}
谢谢罗比。我暂时用'if'代替'while',否则剧本会进入看似无限的循环。我对那里发生的事情有点困惑,因为没有错误出现,尽管它超时,它似乎还没有达到! 我会去你的卷曲的建议,非常感谢示例代码。我现在会尝试。 p.s.对延迟回复表示歉意。 –
真棒谢谢你,半小时调整卷曲设置,它的工作!将我的最终代码放在上面的问题中,仅供将来参考。感谢你的帮助。 –
不客气 - 并感谢发布反馈/结果 - 总是有用的看看。 – Robbie