Solved

html form to REST URL to JSON to PHP

Posted on 2014-09-25
7
342 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

726 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