用PHP解析一个活的SOAP数据流
我有一个企业应用程序提供了一个相当强大的API来收集数据.目前我正在查询每个用户,我想要每秒钟循环一次更新.然而,新的API文档现在提供了所有用户的所有更改的直播.我想知道如何使用php解析这个实时数据.这里有一些细节:
通过SOAP请求请求数据,我正在使用PHP来执行这些请求(返回的唯一ID的会话启动的示例): //Get a session ID for this user for the shoretel WEBAPI $soap_url = 'http://11.11.11.11:8070/ShoreTelWebSDK?wsdl'; $client = new SOAPClient($soap_url,array( 'proxy_host' => '11.11.11.11','proxy_port' => 8070,'trace' => 1 ) ); $client = new SoapClient($soap_url); $header = new SoapHeader('http://www.ShoreTel.com/ProServices/SDK/Web'); $client->__setSoapHeaders($header); $registered_string = $client->RegisterClient(array( 'clientName' => '11.11.11.211' )); $registered_string = get_object_vars($registered_string); $session = $registered_string['RegisterClientResult']; 新方法允许我指定一个时间段.例如,如果我想获得所有事件1分钟,则呼叫将启动,等待一分钟,然后返回在该分钟内发生的所有事件. 我想做的是抓住每个事件发生,并将其插入数据库.这是我可以用PHP完成的东西,还是需要寻找另一种语言来实现这一点?
好的,目标是用“超时”和/或“间隔”来“流式传输SOAP响应”?
我建议覆盖SoapClient __doRequest()方法,并通过fsockopen()实现一个自定义连接,并使用stream_get_contents()流传输数据. 以下代码提供了一个SoapClientTimeout类,其中超时通过stream_set_timeout()设置.这是用例,当服务器响应缓慢,并且您想要确定何时结束侦听. 我建议玩它,并调整套接字上的超时行为. $timeout = 60; // seconds stream_set_blocking($socket,true); stream_set_timeout($socket,$timeout); 当你有一个流,1分钟后关闭, class SoapClientTimeout extends SoapClient { public function __construct ($wsdl,$options = null) { if (!$options) $options = []; $this->_connectionTimeout = @$options['connection_timeout'] ?: ini_get ('default_socket_timeout'); $this->_socketTimeout = @$options['socket_timeout'] ?: ini_get ('default_socket_timeout'); unset ($options['socket_timeout']); parent::__construct($wsdl,$options); } /** * Override parent __doRequest and add "timeout" functionality. */ public function __doRequest ($request,$location,$action,$version,$one_way = 0) { // fetch host,port,and scheme from url. $url_parts = parse_url($location); $host = $url_parts['host']; $port = @$url_parts['port'] ?: ($url_parts['scheme'] == 'https' ? 443 : 80); $length = strlen ($request); // create HTTP SOAP request. $http_req = "POST $location HTTP/1.0rn"; $http_req .= "Host: $hostrn"; $http_req .= "SoapAction: $actionrn"; $http_req .= "Content-Type: text/xml; charset=utf-8rn"; $http_req .= "Content-Length: $lengthrn"; $http_req .= "rn"; $http_req .= $request; // switch to SSL,when requested if ($url_parts['scheme'] == 'https') $host = 'ssl://'.$host; // connect $socket = @fsockopen($host,$port,$errno,$errstr,$this->_connectionTimeout); if (!$socket) { throw new SoapFault('Client',"Failed to connect to SOAP server ($location): $errstr"); } // send request with socket timeout stream_set_timeout($socket,$this->_socketTimeout); fwrite ($socket,$http_req); // start reading the response. $http_response = stream_get_contents($socket); // close the socket and throw an exception if we timed out. $info = stream_get_meta_data($socket); fclose ($socket); if ($info['timed_out']) { throw new SoapFault ('Client',"HTTP timeout contacting $location"); } // the stream contains XML data // lets extract the XML from the HTTP response and return it. $response = preg_replace ( '/ A # Start of string .*? # Match any number of characters (as few as possible) ^ # Start of line r # Carriage Return $ # End of line /smx','',$http_response ); return $response; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- php – 如何确保PDO的lastInsertId()不是另一个同时插入的?
- PHPmailer重复电子邮件问题 – 使用$mail-> Send()的条件语
- php – 当我尝试通过控制面板打开xampp shell时,应用程序无
- Junk-Mail Filter 【并查集虚父节点】
- 关于php 接口问题(php接口主要也就是运用curl,curl函数)
- PHP中用header图片地址 简单隐藏图片源地址
- PHP:彼此使用的类
- 剑指offer 面试题21.22―栈操作以及判断弹出序列
- 奇怪的错误使用PHP简单的HTML DOM解析器
- 时区 – 对于strftime还是仅仅PHP,’US / Eastern’和’US