开发者

file_get_contents not waiting

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:

$getP开发者_如何学运维ersonNamesQuery = <<< 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


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&...."));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜