Solved

html form to REST URL to JSON to PHP

Posted on 2014-09-25
7
334 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 109

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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 59
Exporting image to excel from php document 10 27
Google Maps API and PHP 25 27
ASP server side get value 15 19
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

785 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