PHP字典小部件不起作用

时间:2013-02-07 05:56:30

标签: php curl xml-parsing

我正在尝试创建一个字典小部件,我在其中实现了Dictionary.com的API。我创建了一个包含文本框和文本框的html表单。提交按钮。我正在尝试编写将模拟字典搜索的代码。代码不起作用,我不知道我做错了什么。

这是我正在使用的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-curl。我正在使用的API可以在http://developer.dictionary.com/找到。任何建议将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分: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);