简体   繁体   English

无法将从API提取的数据显示为HTML格式

[英]Unable to display extracted data from API into HTML form

I started using the AlchemyAPI key to extract json/xml data from url's. 我开始使用AlchemyAPI密钥从URL提取json / xml数据。 The alchemy url api key works just great when entered along with the queried url but I wanted the url part to come from the user side so first I created a form with the following code: 炼金术URL api密钥与查询的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>

Then using PHP passed the user's submitted url into the endpoint of the alchemy api and stored the json data into a variable and later displayed the parsed data using the following code (Problem: Code actually doesn't display anything): 然后使用PHP将用户提交的url传递到alchemy 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&quotations=1&url='.$url.'");
  $response = json_decode($response);
  echo $response;}
  echo "<br/> this is not working";
?>

The basic alchemyAPI url I used looks like this (drupal url added at the end) : http://access.alchemyapi.com/calls/url/URLGetRankedTaxonomy?apikey=1f324507a9d51694a29e14f970ccc83de9df2&outputMode=jsonp&knowledgeGraph=1&extract=taxonomy&url=https://www.drupal.org/node/2148541 我使用的基本alchemyAPI url看起来像这样(末尾添加了重复的url): http ://access.alchemyapi.com/calls/url/URLGetRankedTaxonomy?apikey=1f324507a9d51694a29e14f970ccc83de9df2&outputMode=jsonp&knowledgeGraph=1&extract=**taxonomy 。 drupal.org/node/2148541

I just started working with API's and Any help in displaying the parsed json data into html form will be of great help. 我刚刚开始使用API​​,并且将解析后的json数据显示为html形式的任何帮助都会有很大帮助。 Thanks in advance. 提前致谢。 :) :)

"(Problem: Code actually doesn't display anything)" “(问题:代码实际上不显示任何内容)”

That's because everything that's inside this conditional statement will not execute: 这是因为该条件语句中的所有内容都不会执行:

if(isset($_POST['submit'])){...}

Due to the fact that there isn't an input bearing the "submit" name attribute. 由于没有输入带有“提交”名称属性的事实。

What you need to do is to name your submit's input: 您需要做的就是命名提交者的输入:

<input name="submit" type="submit" value="Search">

Had error reporting been set to catch and display on your system, would have thrown an undefined index submit notice. 如果已将错误报告设置为在系统上捕获并显示,则将引发未定义的索引提交通知。

Reference: 参考:

Also make sure that $_GET['go'] is populating correctly. 还要确保$_GET['go']正确填充。 Error reporting will also let you know if it is or isn't. 错误报告还会使您知道是否存在。

Sidenote: 边注:

Having used an else{...} against your opening if{...} conditional statement, would have fired up. if{...}条件声明中使用了else{...}不是您的开头,则可能会触发。

You should also check against $_POST['Search'] for content, if it's set or not empty. 您还应该检查$_POST['Search']的内容(如果设置为空)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM