file_get_contents 不等待

发布于 2024-12-08 06:02:05 字数 1067 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海 2024-12-15 06:02:05

同样的事情也发生在我身上,我使用 http_build_query 来构建查询字符串,而 file_get_contents 和 cURL 的结果都过早响应。在这两个函数中传递完整的查询字符串而不使用 http_build_query 都会成功。诡异的!

结果是被截断的数据:

$pUrl=array('username'=>'_username_',
            'variable'=>'value',
            'variable2'=>'value2',
            'variable3'=>'value3');
$cURL=http_build_query($pUrl);
print_r(file_get_contents("http://www.example.com/api/?$cURL"));

每次都会返回完整的数据:

print_r(file_get_contents("http://www.example.com/api/?username=_username_&variable=value&...."));

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:

$pUrl=array('username'=>'_username_',
            'variable'=>'value',
            'variable2'=>'value2',
            'variable3'=>'value3');
$cURL=http_build_query($pUrl);
print_r(file_get_contents("http://www.example.com/api/?$cURL"));

This returned the complete data everytime:

print_r(file_get_contents("http://www.example.com/api/?username=_username_&variable=value&...."));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文