我开始使用AlchemyAPI键从url中提取json/xml数据。当与查询的url一起输入时,炼金术url键工作得很好,但是我希望url部分来自用户端,所以首先我用以下代码创建了一个表单:
<form method="post" class="SearchCSS" action="/NENSearch.php?go" id="categorizer">
<h1>Enter your Queries</h1>
<input type="text" name="Search" placeholder="Enter the article URL">
<input type="submit" value="Search">
</form>
然后,使用PHP将用户提交的url传递到炼金术api的端点,并将json数据存储到变量中,然后使用以下代码显示解析的数据(问题:代码实际上不显示任何内容):
<?php
echo "this works here";
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("%^((http?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i", $_POST['Search'])){
$url=$_POST['Search'];}
echo $url;}
$response = file_get_contents("http://gateway-a.watsonplatform.net/calls/url/URLGetCombinedData?extract=page-image,entity,keyword,taxonomy&apikey=1f324507a9d516d9429e14f970ccc83de9df2&showSourceText=1&sentiment=1&outputMode=json"ations=1&url='.$url.'");
$response = json_decode($response);
echo $response;}
echo "<br/> this is not working";
?>
我使用的基本alchemyAPI url如下所示(最后添加了http://access.alchemyapi.com/calls/url/URLGetRankedTaxonomy?apikey=1f324507a9d51694a29e14f970ccc83de9df2&outputMode=jsonp&knowledgeGraph=1&extract=taxonomy&url=https://www.drupal.org/node/2148541 ):http://access.alchemyapi.com/calls/url/URLGetRankedTaxonomy?apikey=1f324507a9d51694a29e14f970ccc83de9df2&outputMode=jsonp&knowledgeGraph=1&extract=taxonomy&url=https://www.drupal.org/node/2148541
我刚刚开始使用API,任何将解析的json数据显示为html表单的帮助都会有很大帮助。提前谢谢。:)
发布于 2015-10-30 03:34:52
“(问题:代码实际上什么都不显示)”
这是因为这个条件语句中的所有内容都不会执行:
if(isset($_POST['submit'])){...}
由于没有带有"submit“name属性的输入。
您需要做的是将提交的输入命名为:
<input name="submit" type="submit" value="Search">
如果将错误报告设置为在系统上捕获和显示,则会引发未定义的索引提交通知。
参考资料:
还要确保$_GET['go']
正在正确地填充。错误报告也会让你知道它是或不是。
西德诺特:
如果对打开的else{...}
条件语句使用了if{...}
,就会触发。
您还应该检查$_POST['Search']
的内容,如果它是设置的或不是空的。
https://stackoverflow.com/questions/33432599
复制