2013-02-07 4 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

관련 문제