Solved

PHP Post Request JSON

Posted on 2011-02-18
12
447 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xjs: how to get sum of value in for loop 2 18
Link failure 16 34
SQL querys that gives me from one table into another. 2 24
Copying table data to one another 15 31
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

860 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