2011-10-04 89 views
2

我使用PHP調用REST服務從本地主機上運行的eXist XML數據庫檢索信息,使用file_get_contents()捕獲結果,並使用這些結果填充查詢中的下拉列表形成。我認爲file_get_contents()應該自動等待完整的結果,但顯然它不;有時候這個列表是完全填充的,有時會被截斷。截斷髮生在不同的位置,並重新加載頁面(重新運行PHP,因此REST調用)通常會修復它,儘管有時候並不是第一次嘗試。file_get_contents不等待

如果我正確地診斷問題作爲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); 

感謝,

大衛

+2

的file_get_contents DOES等待響應。如果它很快返回,那是因爲請求的資源很快完成,或者導致連接提前結束。 –

+0

這並不是說它很快就會返回,而是我沒有得到完整的結果,也就是說,返回(有時)會被截斷。如果我誤診了這個問題(當然可能!),那麼我可以在哪裏尋找解釋? – obdurodon

+0

使用可以提供更好的診斷功能的東西,比如捲曲。 file_get_contents有點單片,並且很難調試。但簡而言之,如果它所返回的數據被截斷,那是因爲無論發送它還是網絡,都會導致截斷。 –

回答

1

同樣的事情發生在我身邊,我是用http_build_query建立查詢字符串,和的file_get_contents的結果和捲曲兩者都是過早迴應。在兩個函數中傳遞沒有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&...."));