简体   繁体   中英

Unable to display extracted data from API into HTML form

I started using the AlchemyAPI key to extract json/xml data from url's. 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:

<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
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

I just started working with API's and Any help in displaying the parsed json data into html form will be of great help. 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. 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.

You should also check against $_POST['Search'] for content, if it's set or not empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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