How do I perform REST queries in (cake)php? - php

I'm trying to pull some data into my CakePHP site using a REST API. I'm familiar with CakePHP, but not with REST, so I'd like some guidance as to the best way to do this. I've looked online, but a lot of what I find is either telling me how to make my own API, or it's quite old, and I'm not sure if it's still relevant.
The API documentation tells me to make the query request
http://api.sitename.com/rest/organizations/my_id/media.xml
where my_id is a string that I have.
I'm using:
$url = http://api.sitename.com/rest/organizations/my_id/media.xml;
$result = file_get_contents($url);
and then I have the XML in $result.
Is this the best way to do this?

Short, direct answer to:
Is this the best way to do this?
No
Head over to the PHP cURL documenation for more info on how to "talk" to public web services.

After looking into cURL as Charles Sprayberry suggests, you'll then want to learn about parsing XML in PHP to be able to do something with the data that you retrieve.

I found this article to be useful for understanding a bit about Cake and ReST:
http://nuts-and-bolts-of-cakephp.com/2010/06/27/putting-semi-restful-api-development-to-rest/

Related

Building Simple REST API using PHP without framework

I want to build a REST API using PHP, but without any framework. By the following requirments:
The code should be as simple as possible with OOP development principles in mind, easy to read and expand
Data should be kept in MySQL and to be returned as JSON in the given format
DO NOT use ANY Framework or ANY already written code, but to have structure
User input data validation
There should be no security issues
At first, I thought I should build complete MVC project, but I realized that actually I will probably don't need any views and I will use Services instead of controllers. And also models for both entities (Articles and Users).
I'm still not sure what is the perfect way to do it, so I will just tell you what I`m thinking so far...Sorry if Its a duplicate post but I haven't found much information about this and from the little I found, I got more confused.
I thinking of a simple router.php class that will have a method:
map($httpMethod, $route, $callback)
So, for example, I will call ("POST", "/users/register", registerUser(params)) or ("GET", "/users", registerUser(params)), just like I would do in a MVC web app.
I think I will need a model and a service for each of both entities. The service will execute the SQL for each CRUD operation. I think I know how to create the service, as it won't be much different than a controller.
But I wonder how can I create the model part for both entities. What exactly I will need for the models as a code?
First of all, it would be nice if you agree that this is the right way and if not, I would love to hear a lot of criticism because I'm currently confused and really don't know where to start.
I agree with you.
And suggest you to know about Loopback, it's good, like what you described.
I know your question is how to build your own rest api without framework, but it sounds like you'd actually make good use of at least some components (not necessarily a whole framework), do you really need/want to write a router from beginning?
if so sure, if no maybe some microframework? anyway, symfony has some info on how you would create your own framework (just as an example), they use couple of their own classes (i.e. httpcomponent), but just for the explanation of idea/way how things you want are done.
https://symfony.com/doc/current/create_framework/index.html
I found this library in PHP for get started with REST API's
php-platform/restful
This requires prior knowledge on using Composer

Using Google Adwords API in PHP

Hi there I was wondering if anyone could help me out.
Basically I am trying to use the trafficEstimatorService in order to retrieve how much traffic a keyword that is entered through a HTML generates.
I have an idea of how to make it work but I am totally new to using the google API.
Firstly would I create the form in php and perform a GET request on the trafficEstimator url with the keywords in the form as parameters?
Thanks!
I found that the hardest part of getting started was the Oauth Authentication. Ewan Hemming's tutorial on the subject helped me out a lot: http://www.ewanheming.com/adwords-api-oauth-tutorial
Take a look at https://github.com/googleads/googleads-php-lib
It's a php client library including examples on how to do things + a video which is a very good and detailed starting point.
If you already tried to follow Make Your First API Call and video with talk through instead of walkthrough of the steps and you felt confused and unclear, then here is step by step tutorial for you.

PHP RESTful Web Service for an iPhone

I'm developing an iPhone APP and need to implement also an Web Service.
First of all I'm not a Developer and never made something big in PHP, Objective-C, xCode.
My PHP knowledge isn't also good. But let's start with my Environment.
iPhone APP (xCode 4.2, iOS5), PHP Web Service, MySQL DB
I was researching the WEB and most People tend more to REST than SOAP. I think i see also the advantages of REST (using of simple HTTP Verbs (get, post, delete etc...), but that's not the main point here...
I think I understand the main goal of the REST Architecture and tried to make a little concept with an URI and Verb Mapping. Here just a simple example of the mapping:
/location/{location_id}/product
/location/{location_id}/product/{product_id}
Both are GET operations who should get me ether a single product or all products of a location.
How would a simple PHP REST Web Server look like with these functions?
Another part should implement a User Authentication from the iPhone. Somehow i need to store the user session, right now I don't have any idea how to make that. The goald is that if only a user is logged in, he could review the product.
Now I've researched also the Web but couldn't find an easy step-by-step Tutorial.
Do you know any good Tutorials which will help me achieve my goal? :)
A lot of people prefer using PHP Frameworks like ZEND. This seems very interesting, but it seems like a big package with a lot of modules.
Does someone know exactly which Modules are needed to get my Web Service working?
This is quite a good tutorial, it uses the codeigniter framework which makes the learning curve a bit steeper but makes it a lot more powerful in the long run.
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
If you want to build this the custom way it's actually very easy to do if you want to return information in the JSON format especially for php5 which is generally well supported amongst hosts these days.
Essentially the steps are like this:
Pass in product id via url and retrieve using GET i.e. service.php?product_id=10
Query database and return data for product id that was passed in
Store returned data in an array
Set header content-type to application/json
json_encode the result (json_encode)
That way when you call that url in a browser you will get a nice JSON formatted array result in a key:value pair manner. And as of iOS5 json parser comes with the framework (for earlier versions SBJson is a good framework to use (SB JSON))

CakePHP Rest Plugin Question

I am new to REST. I understand how to create the functions within the application. BUT I am not sure how to access the JSON request using REST with headers. I am using CakePHP as my framework and in addition to that I am using the CakePHP REST plugin by KVZ (https://github.com/kvz/cakephp-rest-plugin). I am using ACL within my application and therefore I need to login using REST headers or however you access that information. I am pretty sure your suppose to use the amazon S3 authentication method but I don't understand it. I haven't been able to find any clear information on this.
THE QUESTION: How do I access the function (http://localhost:5555/app/data/getAll.json) using a REST JSON Request with PHP? Please include how to send the username and password with HTTP_AUTHORIZATION/header. The more detailed the answered, the better.
Thanks
REST isn't a technology, its just a way of naming your urls. Have a look at http://www.xfront.com/REST-Web-Services.html. You can use any mechanism you like to access the page although Curl with PHP is probably the easiest way for you.
Have a look at the PHP curl docs, there are examples there to guide you.

Pubsubhubbub handle in php

I need to watch a certain feed for my web app and i can't find any proper documentation about this topic
-Edited:
what i'm actually after is probably so simple that it might be embarrassing :)
i know the hub will use a POST request to deliver the updates but i don't know how to fetch that data using $_POST because i don't know the key value in the $_POST array
I'd love to see basic PHP code example on how this is done
thanks
I don't know how good it is, but there is an implementation being developped in Zend Framework ; it's currently still in incubator, but maybe it might interest you : /standard/incubator/library/Zend/Feed/Pubsubhubbub
OK
problem solved, i found the protocol wrappers section at php.net.
"php://input" is what i was looking for
http://www.php.net/manual/en/wrappers.php

Categories