Solved

html form to REST URL to JSON to PHP

Posted on 2014-09-25
7
351 Views
Last Modified: 2014-10-02
Hello Experts

I'm using a simple html form

<form method="post" action="myform.php">
<input type="submit" value="Submit">
<input type="text" name="varX">
</form>

At this moment this form outputs varX to myform.php

But i need varX to first go through a REST URL that produces a JSON

URL: http://some_external_site.com/?var=varX

This url outputs this JSON

{
 "var1": "success",
 "var2": "varX",
 "var3": false
}

I want var1, var2, var3 put into var's in myform.php so i can use it for other stuff.

I do not know how to begin with this.
But you guys do ?

Kind Regards
0
Comment
Question by:Th0R
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 40345046
PHP can convert the responses to JSON without a third party processor. See reference.

What I don't understand is how you go from varX (one field) to var1, var2, var3 (three fields). Can you elaborate?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40345065
<?php // demo/temp_thor.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28525896.html

if (!empty($_POST))
{
    $obj = new StdClass;
    $obj->var1 = "success";
    $obj->var2 = $_POST['varX'];
    $obj->var3 = FALSE;
    $str = json_encode($obj);
    echo htmlentities($str);
}
$form = <<<EOD
<form method="post" action="">
<input type="submit" value="Submit">
<input type="text" name="varX">
</form>
EOD;

echo $form;

Open in new window

0
 

Author Comment

by:Th0R
ID: 40345598
Hello xmediaman
The rest url is an api that returns those 3 vars in a json file when called with varX . See the url i mentioned.
this url is at an external site and is already operational

i hope im making myself clear enogh
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 22

Expert Comment

by:Kim Walker
ID: 40345906
It sounds like you need myform.php to accept varx, then send it to the REST URL and process the returned JSON object. Unfortunately this is beyond my experience, but if this explanation is accurate, it might help Ray and other experts to present a solution to you.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40346015
The rest url is an api...
What is the URL? What is the API?  Please give us clear specific information, if the code snippet located here somehow did not work as expected.  We are experts but not mind readers and the more accurate and complete information you can give us, the better!
0
 

Assisted Solution

by:Th0R
Th0R earned 0 total points
ID: 40347755
ok i found it.

  $app_info = file_get_contents('http://some_external_site.com/?var=' . $varX);
  $app_info = json_decode($app_info, true);

thnx!
0
 

Author Closing Comment

by:Th0R
ID: 40356567
ok i found it.

  $app_info = file_get_contents('http://some_external_site.com/?var=' . $varX);
  $app_info = json_decode($app_info, true);

thnx!
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question