Solved

PHP Post Request JSON

Posted on 2011-02-18
12
444 Views
Last Modified: 2012-05-11
Hi all,

I need some example code to be able to get some info from an api.

This is what I've got from the api:

Method is:

http://www.mydomain.com/api/set/

and

http://www.mydomain.com/api/get/

Parameters/ Example Request

{
    'site': 'mysite',
    'id': 'some_id',
    'stage': 2,
}

and for the get

{
    'site': 'mysite',
    'id': 'some_id',
}

example resplonses would be:

for set:

{"status": "successful", "data": {}}

and for get:

{"status": "successful", "data": {stages: [1,1,0,1]}}


I just need some client code to get and set the information from the api ...

Hope all this info helps as it's all I have.

Thanks

0
Comment
Question by:error77
  • 7
  • 3
  • 2
12 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34924590
check jquery ajax
http://viralpatel.net/blogs/2009/04/jquery-ajax-tutorial-example-ajax-jquery-development.html
http://api.jquery.com/jQuery.ajax/

for example, for get

$.get(
    "http://www.mydomain.com/api/get/",
    "{
        'site': 'mysite',
        'id': 'some_id',
    }",
    function(data) { alert(eval(data).status); }
);


Similarly for set
0
 

Author Comment

by:error77
ID: 34924645
OK, I've done the above and to call the function added this:

<input type="button" value="Click Here" onClick="data();">

but nothing happens ... anything I'm doing wrong?

Thanks

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34924684
what is data() ??

and what exactly do you want to do with the data?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:error77
ID: 34924700
I want to go to that url and have some data returned ... for new an alert message with the return will do.

0
 

Author Comment

by:error77
ID: 34924721
In other word...post a request and get some data returned so I can then use them.
0
 

Author Comment

by:error77
ID: 34924728
As for your question;  What is data ...

I got it from your code:

$.get(
    "http://www.mydomain.com/api/get/",
    "{
        'site': 'mysite',
        'id': 'some_id',
    }",
    function(data) { alert(eval(data).status); }
);

function(data)  <-- so I guess the function was called data?
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 34924793
no this function is not data.
In fact this is an anonymous callback handler

You need to put your get handler in data method to call it that way

function data()
{
 $.get(
    "http://www.mydomain.com/api/get/",
    "{
        'site': 'mysite',
        'id': 'some_id',
    }",
    function(data) { alert(eval(data).status); }
);
   
}
0
 

Author Comment

by:error77
ID: 34924832
OK, tried that but nothing is popup up .. how do I get a popup out this with the return please?

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34925295
What is the actual URL of the web service?  Please post some test data so we can test the code to access the service.  Thanks.
0
 

Author Comment

by:error77
ID: 34926122
Sorry, I can't do that :o/
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34926161
Well, if you can't post the actual URL, at least take the time to set up a test case that works the same way.  You're asking us for assistance - it would seem reasonable that you would give the fellow experts something more tangible than "please help me get my data right and btw, I will not let you see you my data."
0
 

Author Closing Comment

by:error77
ID: 34926166
My mistype. It's now working fine thanks.
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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 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)

815 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

13 Experts available now in Live!

Get 1:1 Help Now