2013-02-07 46 views
0

我想創建一個字典小部件,我在其中實現Dictionary.com的API。我創建了一個HTML表單,其中包含一個文本框&提交按鈕。我正在嘗試編寫模擬字典搜索的代碼。代碼不起作用,我不知道我做錯了什麼。PHP字典小部件不工作

這裏是我使用的PHP代碼:

<?php 

    $vid = "<I ENTER THE VID HERE AS A STRING>"; 

    $url = "http://api-pub.dictionary.com/v001?vid=" . $vid . "&q=" . $_POST['dictionary_search'] . "&type=define&site=dictionary"; 

    // initialize curl + store in a variable 
    $ch = curl_init(); 

    // configure cURL 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    $result = curl_exec($ch); 

    echo $result; 
?> 

我啓用了PHP的捲曲。我正在使用的API可以在這裏找到http://developer.dictionary.com/。任何建議將不勝感激。提前致謝。

+0

是作爲JSON數據返回? – Techie

+0

數據以xml形式返回 – JaPerk14

回答

0

方法1

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,'http://example.com/path/here'); 
    curl_setopt($ch, CURLOPT_FAILONERROR,1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
    $retValue = curl_exec($ch);   
    curl_close($ch); 

$oXML = new SimpleXMLElement($retValue); 

foreach($oXML->entry as $oEntry){ 
    echo $oEntry->title . "\n"; 
} 

方法2

$json_url ='https://exampleurl'; 

// Initializing curl 
$ch = curl_init($json_url); 

// Configuring curl options 
$options = array(
CURLOPT_RETURNTRANSFER => true 
); 

// Setting curl options 
curl_setopt_array($ch, $options); 

// Getting results 
$results = curl_exec($ch); // Getting jSON result string 

$xml = simplexml_load_string($results); 
print_r($results); 
+0

有兩個問題:我可以將以下url放入download_page函數嗎? http://api-pub.dictionary.com/v001?vid= &q = hello&type = define&site = dictionary – JaPerk14

+0

第二個問題:我不知道xml數據的確切結構,所以如何查看所有的xml ? – JaPerk14

+0

檢查更新的答案 – Techie