我正在构建的基于CodeIgniter的应用程序的一部分遇到了问题。
我必须连接到远程服务器,并从第三方获得关于如何打开套接字、发送一些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);
}发布于 2012-09-21 11:56:32
不确定错误,但你需要while (!$feof($fp)),而不是'if‘- while将循环,直到你得到所有的响应。这仍然不能解决你的问题所以..。
如果使用curl实现,您可能会发现这更容易。更短,更整洁,更容易调试。http://php.net/manual/en/book.curl.php
<?php
$ch = curl_init();
// Set the various options. replace the xxxx's
curl_setopt($ch, CURLOPT_URL,"xxxxxxxxxxxx.asp");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml_in=' . $xmlString);
curl_setopt($ch,CURLOPT_USERAGENT,'xxxxxxxxxxx_socket/1.0');
// Request the header information in the response. You're example gets the header, but you may not need it, so change to false if not
curl_setopt($ch,CURLOPT_HEADER,true);
// Now execute the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
curl_close ($ch);
// Response is the data.
$aLines = explode("\n", $response); // Splits the resposne into an array so you can process individual lines.
// etc
?>https://stackoverflow.com/questions/12523680
复制相似问题