How to troubleshoot Slim REST API by simulating HTTP - php

I am new to http://www.slimframework.com/, but it is easy and I have the basics working. Now I wish to go through some of my script which is initiated by slimframework using an IDE (PhpED).
I suppose I could just set the URL as needed for a GET request, and make a HTML form and submit it for POST requests.
I expect there has to be a better way. How can I specify the method, url, and body so that I can troubleshoot the script?

You can look into a handy tool called Postman.
Once downloaded and open, you can specify URL, body, method and much more, send the request and look at the response. Parsed or unparsed. It lets you add collections so you can actually rebuild your entire API calls.

Related

HTTP Redirect form external_api?

I've got an external_api implemented that I can currently hit the standard way;
http://localhost/webservice/rest/server.php?wstoken=foo&wsfunction=local_test_function&moodlewsrestformat=json
and it returns:
https://google.com/?q=test
The thing I can not figure out is, how can I make this return an HTTP/302? I'm trying to work around the fact that atto won't run JS code, and I thought I could just do an iFrame that has it's src set to my webservice, but all I can seem to do is return a string.
You cannot, Moodle web service API is very much designed around a typical RPC API. You make a call, and you get an answer. There aren't any implementation of HTTP verbs, nor respond codes apart from 500, and 403, when an exception is raised, or permissions are lacking.
I'm actually uncertain why you would want to do this anyway. The external_api is meant to interact with Moodle, not to redirect elsewhere, especially if you end-up passing a user's token to an iframe.

Server to server communication with JSON and PHP

I want to make a small PHP script that will check a web app periodically. The communication will be only in json. The code will run on Google App Engine.
First request will be an HTTP POST request with form data like this: username=user&password=pass
POST http://www.example.com/login HTTP/1.1
If login fails the response json will be:
{
"message": "Failed login"
}
If successful:
{
"response": "OK",
"username": "user",
"protocol": "http"
}
Subsequent requests will be GET, POST and PUT requests containing json and also the response will be in json.
This code will be run by Google App Engine's Cron for PHP ones a day.
Since I have little knowledge of PHP, I would like to know how I should implement this.
How do I make the http request to the web app.
How do I remember login/authentication headers from one request to another.
How do I handle reading, writing and modifying of json in PHP.
All I need is a basic example and guidelines to get me started.
Thank you in advance.
Tase
Given that GAE does not currently support cURL I would recommend an approach like How do I send a POST request with PHP?. Down the road you may be able to use something slick like Guzzle (if GAE adds cURL support).
Use json_decode/json_encode to create JSON to send and parse the responses.
Just to get you started: cURL and json_decode/json_encode.
If all of your requests will be in the same script, run all at the same time, you'll just need to use the cookies and headers options for cURL.
I'll leave a full example to someone else, if necessary.
Also, you should use HTTPS if possible. It's never a good idea to send username/password in clear text, no matter how trivial the service.
Edit: as it looks like GAE doesn't support curl (thanks #boombatower), check this out: Replacing CURL with urlfetch in PHP

Get XML Response using CURL, FOPEN or FILE_GET_CONTENTS in PHP

I would like to create a PHP script that would perform a request to an https site using the GET method. In return, I get a response in xml format. I need to be able to get the contents/save this xml.
I've tried using curl, file_get_contents, fopen but I'm getting a Bad Request (400) response. But when I try to go directly to the url, I receive the xml response. Here's the url that I'm trying to get response with:
https://tst.webservices.outsurance.co.za/SecureHost/Lead/LeadPostService.svc/SubmitAffiliateLead?xml=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22+%3F%3E%0D%0A%0D%0A%3Clead%3E%0D%0A+++%3Cmode%3ELIVE%3C%2Fmode%3E%0D%0A%09%3Ctitle%3EMrs%3C%2Ftitle%3E%0D%0A%09%3Cfirstname%3EHannah%3C%2Ffirstname%3E%0D%0A%09%3Clastname%3EDwindle%3C%2Flastname%3E%0D%0A%09%3Cid%3E1723658492165%3C%2Fid%3E%0D%0A%09%3Chomecode%3E%3C%2Fhomecode%3E%0D%0A%09%3Chometel%3E%3C%2Fhometel%3E%0D%0A%09%3Cworkcode%3E011%3C%2Fworkcode%3E%0D%0A%09%3Cworktel%3E132189%3C%2Fworktel%3E%0D%0A%09%3Cmobile%3E0824176239%3C%2Fmobile%3E%0D%0A%09%3Cemail%3Ehannahdwindle#gmail.com%3C%2Femail%3E%0D%0A%09%3Ccomment%3ETHIS+IS+A+COMMENT%3C%2Fcomment%3E%0D%0A%09%3Csource%3EUPSTART%3C%2Fsource%3E%0D%0A%09%3Cnotes%3ETHIS+I+A+NOTE%3C%2Fnotes%3E%0D%0A%09%3Clanguage%3EE%3C%2Flanguage%3E%0D%0A%09%3Cproduct%3EP%3C%2Fproduct%3E%0D%0A%3C%2Flead%3E%0D%0A+++++++++
I really don't kknow what to do now. I have tried every solution that I've seen on the internet but to no avail.
I wrote this response a while back. It can give you way to debug your outgoing requests.
It looks like your http client is composing a request that the receiving application doesn't like. This can be on the application level too (maybe they just don't like your variables and are returning a 400 level response). I've seen API implementers do that. There's nothing to stop the implementers from doing that.
I'd find out what the exact request is that you're sending (by that link I provided) and contact the API implementers to ensure that they're expecting that type of request.
Also, with fiddler, you can craft http requests. However, the best way to craft requests to test remote APIs is to manage every byte. You can do that by using this method (supports both HTTP and HTTPS).
Lastly, I wrote an XML wrapper to make xml modification/querying easier, if it helps you at all.

Difference between an Output of a normal API and a REST API

What is the difference between a REST API and a normal API (which prints a JSON response)?
There is no difference at all. REST describes a way of interacting with a HTTP server, not what the server should return in response. Most web apps interact with the server side by POST or GET requests with any additional information needed to fulfil the request in a form submission for POST or the query string for GET. So if you want to delete something from the server they typically do POST with a form that contains data that specifies a resource along with an instruction to delete it.
However, HTTP implements methods (also known as verbs) other than GET or POST. It also implements, amongst others, HEAD (return the same headers you would have done for a GET, but with no response body), PUT (Take the request body and store its content at whatever URL the PUT request was made to), and DELETE (Delete whatever resource exists at the specified URL). A REST interface simply makes use of these additional verbs to convay the meaning of the request to the server.
Browsers typically only support GET and POST for "normal" (non-XHR) requests, but tools like Curl can issue the full set of HTTP verbs. You can also use additional verbs with XHR-based techniques such as AJAX.
You will still have to provide a traditional non-REST API for browsers to use, unless you're making javascript and XHR support a requirement for using your app.
REST mostly just refers to using the HTTP protocol the way it was intended. Use the GET HTTP method on a URL to retrieve information, possibly in different formats based on HTTP Accept headers. Use the POST HTTP method to create new items on the server, PUT to edit existing items, DELETE to delete them. Make the API idempotent, i.e. repeating the same query with the same information should yield the same result. Structure your URLs in a hierarchical manner etc.
REST just is a guiding principle how to use URLs and the HTTP protocol to structure an API. It says nothing about return formats, which may just as well be JSON.
That is opposed to, for example, APIs that send binary or XML messages to a designated port, not using differences in HTTP methods or URLs at all.

Possible to use Javascript to get data from other sites?

Is it possible for a web page using Javascript to get data from another website? In my case I want to get it for calculations and graphing a chart. But I'm not sure if this is possible or not due to security concerns. If it is considered a no no but there is a work around I would appreciate being told the work around. I don't want to have to gather this information on the server side if possible.
Any and all help is appreciated.
Learn about JSONP format and cross-site requests (http://en.wikipedia.org/wiki/JSON#JSONP).
You may need to use the "PHP-proxy" script at your server side which will get the information from the websites and provide it to yours Javascript.
The only reliable way is to let "your" webserver act as a proxy. In PHP you can use curl() to fire a HTTP request to an external site and then just echo the response.
You can't pull data from another server due to the same origin policy. You can do some tricks to get around it, such as putting the URL in a <script> tag, but in your case it wouldn't work for just parsing HTML.
Use simple_dom_html, to parse your data server side. it is much easier than doing it in JavaScript anyways.
A simple way you might be able to do this is to use an inline iframe. If the web page you are getting the data from has no headers, or you can isolate the data being pulled in (to say an image or SWF), this might work.
cross-domain javascript used to be impossible, using a (php-)proxy was a workaround for that.
jsonp changes this entirely, it allows to request javascript from another server (if it has an API that supports jsonp, a lot of the bigger webplayers like google, twitter, yahoo, ... do), specifying the callback-function in your code that needs to be triggered to act on the response.
the response in javascript will contain:
a call to a callback-function you defined
the actual payload as a javascript-object.
frameworks like jquery offer easy support for jsonp out of the box.
once you have the raw data you could tie into google chart tools to create graphs on the fly and insert them in your webapp.
Also worth considering is support for XMLHttpRequest Access Control which is support in some modern browsers.
If the service provider that you are trying to access via a web page has this set up, it is a very simple call to XMLHttpRequest and you will get access to the resources on that site without the need for JSONP (especially useful for requests that are not GET, i.e. POST, HEAD etc)

Categories