2010-08-22 71 views
0

我試圖通過API發出調用,以便我可以刪除用戶屬性。當我轉到ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties時,它返回包含所有用戶屬性的XML。通過API調用獲取XML

我想將該XML存儲在變量$ xmlString中,然後我應該可以通過循環來獲取屬性並依次刪除。用戶屬性是鍵和值可以是任何東西的關鍵值,所以不幸的是無法知道所有選項。

至今代碼:

$delete = "http://www.ourwiki.com/@api/DELETE:users/$user_id/properties/%s"; 
$xmlString = file_get_contents('ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties') 
$xml = new SimpleXMLElement($xmlString); 

foreach($xml->property as $property) { 
    $name = $property['name']; // the name is stored in the attribute 
    file_get_contents(sprintf($delete, $name)); 
} 

出於某種原因,的file_get_contents似乎並不能夠得到的XML。我甚至確保在環境中啓用allow_url_fopen。任何幫助,這將不勝感激。

+0

難道你得到的error_reporting設爲E_ALL錯誤/警告信息? $ xmlString('var_dump($ xmlString);')的值是什麼? – VolkerK 2010-08-22 13:41:52

+0

假設我在賦值變量的地方添加var_dump($ xmlString),它返回bool(false)。 – Aaron 2010-08-22 13:53:19

回答

0

嘗試

$xmlString = file_get_contents("http://ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties"); 

在第二行。在開始時不包括協議,PHP正在服務器文件系統中尋找一個文件。

編輯:

你說var_dump($xmlString)返回false。從文檔:

該函數與file()相似,不同之處在於file_get_contents()從字符串中返回文件,從指定的偏移量開始直到maxlen字節。 失敗時, file_get_contents()將返回FALSE。

這意味着PHP不能從GET該URL的任何數據。

嘗試使用

$xmlString = file_get_contents(urlencode('http://ourwiki.com/@api/users/[email protected]/properties')); 
+0

對不起,當我寫這篇文章時忽略了它,但是我在實際測試腳本時確實包含了協議。雖然似乎沒有真正的效果。 – Aaron 2010-08-22 13:47:03

+0

VolkerK詢問的「var_dump」的結果是什麼? – 2010-08-22 13:49:20

+0

我剛剛在上面添加了一條評論,但它正在返回bool(false) – Aaron 2010-08-22 13:53:56