2016-11-02 27 views
0

這不是重複的,因爲我閱讀所有其他問題和答案,並且無法解決我的問題,不同simplexml_load_string =>不允許對'SimpleXMLElement'進行序列化[錯誤] //將xml文件內容從url傳遞給變量

$tcmb_gov_tr = file_get_contents("http://www.tcmb.gov.tr/kurlar/today.xml"); 
     $currency_xml = simplexml_load_string($tcmb_gov_tr); 
     $tmp_currency = array(); 
     if($currency_xml->Currency[0]['Kod']=='USD'){ 
     $_SESSION['currency']['usd_try']['buying'] = $currency_xml->Currency[0]->ForexBuying[0]; 
     $_SESSION['currency']['usd_try']['selling'] = $currency_xml->Currency[0]->ForexSelling[0]; 
     } 

代碼的工作,但仍然拋出

Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in [no active file] on line 0

我不想得到這個錯誤我應該怎麼做,我從XML值作爲數組或對象,你可以建議anohter方法,或者幫助我使用該功能

+1

事實上,你的錯誤是從「第0行」引發的,這意味着它發生在腳本關閉 - 你是否在會話中存儲$貨幣變量? – iainn

+0

不,我不會將它存儲在會話中 – TeknolojiGezgini

+0

我不會將$貨幣直接存儲到會話中,但我將它的值存儲在會話中,爲什麼我不知道但在會話中存儲它的值拋出該錯誤,謝謝iainn,如果你想創建一個答案,我會將其標記爲正確答案。 未在會話中存儲其值,解決了我的問題 – TeknolojiGezgini

回答

0

簡單最佳的解決方案

$_SESSION['currency']['usd_try']['buying'] = (string)$currency_xml->Currency[0]->ForexBuying[0]; 

我發現它保存爲SimpleXMLElement對象,當我把(字符串)XML元素,會話變量存儲爲字符串,則沒有錯誤了。 (string)將xml元素調用爲字符串函數。