Solved

PHP Post Request JSON

Posted on 2011-02-18
12
442 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
Comment Utility
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
Comment Utility
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
Comment Utility
what is data() ??

and what exactly do you want to do with the data?
0
 

Author Comment

by:error77
Comment Utility
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
Comment Utility
In other word...post a request and get some data returned so I can then use them.
0
 

Author Comment

by:error77
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
Comment Utility
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
Comment Utility
OK, tried that but nothing is popup up .. how do I get a popup out this with the return please?

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Sorry, I can't do that :o/
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
My mistype. It's now working fine thanks.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

6 Experts available now in Live!

Get 1:1 Help Now