PHP字典小部件不工作
我正在尝试创建一个dictionary小部件,在其中实现dictionary.com的API。我创建了一个包含文本框和提交按钮的html表单。我正在尝试编写模拟字典搜索的代码。代码不起作用,我不知道我做错了什么 以下是我正在使用的PHP代码:PHP字典小部件不工作,php,curl,xml-parsing,Php,Curl,Xml Parsing,我正在尝试创建一个dictionary小部件,在其中实现dictionary.com的API。我创建了一个包含文本框和提交按钮的html表单。我正在尝试编写模拟字典搜索的代码。代码不起作用,我不知道我做错了什么 以下是我正在使用的PHP代码: <?php $vid = "<I ENTER THE VID HERE AS A STRING>"; $url = "http://api-pub.dictionary.com/v001?vid=" . $vid .
<?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可以在这里找到。如有任何建议,将不胜感激。提前感谢。方法1
方法2
数据是否以JSON的形式返回?数据以XML的形式返回两个问题:我可以将以下url放入下载页面函数中吗?第二个问题:我不知道xml数据的确切结构,因此如何查看所有xml?在您的第一个示例中,似乎只有在您知道xml的组成部分时,代码才起作用。我尝试使用您的第一个示例代码,但收到一个错误:警告:SimpleXMLElement::\uu construct():Entity:第1行:解析器错误:StartTag:元素名称无效。它表示错误是针对$oXML=newsimplexmlement($retValue)的行;
$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";
}
$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);