Solved

html form to REST URL to JSON to PHP

Posted on 2014-09-25
7
330 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 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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

919 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

11 Experts available now in Live!

Get 1:1 Help Now