file_get_contents 不等待
我使用 PHP 调用 REST 服务来从 eXist XML 数据库检索信息,在本地主机上运行,使用 file_get_contents()
捕获结果,并使用这些结果填充下拉列表以查询形式。我认为 file_get_contents() 应该自动等待完整的结果,但显然它没有;有时列表已满,有时会被截断。截断发生在不同的位置,重新加载页面(重新运行 PHP,因此重新运行 REST 调用)通常可以修复它,尽管有时第一次尝试时无法修复。
如果我正确诊断了问题,因为 file_get_contents()
不等待结果,任何人都可以建议我如何修复它吗?或者还有其他解释吗?这是 PHP 的相关片段:
$getPersonNamesQuery = <<< EOQ1 {for \$i in doc('/db/genealogy/genealogy.xml')//person[not(.//firstName eq "unknown")] order by string-join(\$i/name/*," ") return {normalize-space(concat( \$i/name/firstName, " ", if (\$i/name/epithet) then concat("â",\$i/name/epithet,"â) else "", " ", \$i/name/patronymic," ", if (not(\$i/@origin eq "Rus'" or \$i/@origin eq "unknown")) then concat("of ",\$i/@origin) else "" ))} } EOQ1; $contents = "http://localhost:8080/exist/rest/db/genealogy?_howmany=10000&_wrap=no&_query=" . urlencode($getPersonNamesQuery); $personNames = file_get_contents($contents);
谢谢,
David
I'm using a PHP call to a REST service to retrieve information from the eXist XML database, running on localhost, capturing the results with file_get_contents()
, and using those results to populate a drop-down list in a query form. I thought that file_get_contents()
was supposed to wait automatically for complete results, but apparently it doesn't; sometimes the list is fully populated and sometimes it's truncated. The truncation happens at various locations, and reloading the page (rerunning the PHP, and therefore the REST call) usually fixes it, although sometimes not on the first try.
If I've diagnosed the problem correctly as a matter of file_get_contents()
not waiting for results, can anyone advise me about how to fix it? Or is there an alternative explanation? Here's the relevant snippet from the PHP:
$getPersonNamesQuery = <<< EOQ1 {for \$i in doc('/db/genealogy/genealogy.xml')//person[not(.//firstName eq "unknown")] order by string-join(\$i/name/*," ") return {normalize-space(concat( \$i/name/firstName, " ", if (\$i/name/epithet) then concat("â",\$i/name/epithet,"â) else "", " ", \$i/name/patronymic," ", if (not(\$i/@origin eq "Rus'" or \$i/@origin eq "unknown")) then concat("of ",\$i/@origin) else "" ))} } EOQ1; $contents = "http://localhost:8080/exist/rest/db/genealogy?_howmany=10000&_wrap=no&_query=" . urlencode($getPersonNamesQuery); $personNames = file_get_contents($contents);
Thanks,
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
同样的事情也发生在我身上,我使用 http_build_query 来构建查询字符串,而 file_get_contents 和 cURL 的结果都过早响应。在这两个函数中传递完整的查询字符串而不使用 http_build_query 都会成功。诡异的!
结果是被截断的数据:
每次都会返回完整的数据:
Same thing happened with me, I was using http_build_query to build query string, and the result of file_get_contents and cURL both were responding prematurely. Passing the complete query string without http_build_query in both of the functions resulted in a success. Weird!
This resulted is truncated data:
This returned the complete data everytime: