Solved

html form to REST URL to JSON to PHP

Posted on 2014-09-25
7
327 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 21

Accepted Solution

by:
Kim Walker earned 500 total points
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
<?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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP Sum Column in Table 3 25
Urgent need help ASAP With CSS 5 19
Bootstap Icons 3 17
Converting SQL server date to string 3 15
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now