PHP uses restful APIs

Source: Internet
Author: User
How RESTful APIs work with PHP
For example, I need to do 1 upload images to a standalone image server

As below, the RESTful API reference
Https://github.com/alibaba/nginx-tfs/blob/master/TFS_RESTful_API.markd ...
Grammar

Post/v1/appkey http/1.1

host:10.0.0.1:7500

Content-length:length

Date:date

PUT/V1/TFS/T1FOZHB4ET1RCVBVDK http/1.1
host:10.0.0.1:7500
Content-length:22
Date:fri, 03:05:00 GMT

[Data]

How should this be written in PHP? Any reference?

Reply content:

How RESTful APIs work with PHP
For example, I need to do 1 upload images to a standalone image server

As below, the RESTful API reference
Https://github.com/alibaba/nginx-tfs/blob/master/TFS_RESTful_API.markd ...
Grammar

Post/v1/appkey http/1.1

host:10.0.0.1:7500

Content-length:length

Date:date

PUT/V1/TFS/T1FOZHB4ET1RCVBVDK http/1.1
host:10.0.0.1:7500
Content-length:22
Date:fri, 03:05:00 GMT

[Data]

How should this be written in PHP? Any reference?

Because RESTful APIs are HTTP-based, that is, you can communicate with the API as long as you have an HTTP client (such as a browser, simulating with PHP's curl library, and so on).

All you see is some HTTP header information and data when the client and API communicate based on the HTTP protocol, such as when you open the Developer tool for Google Chrome and then select Network You can see all the HTTP-related information for this communication.

Before making an API call, it is important to understand some of the relevant knowledge of HTTP and REST first.

The simple principle is this: since the client (your app) communicates with the API, you must use the HTTP protocol, which is simple. You just send an HTTP request to the API. How to send? Browser on the line, of course, it is convenient to use some plug-ins (later said). The point is that you're now going to use PHP to send HTTP requests and processing requests. Well, PHP provides the Curl library and the Fsockopen, both of which can simulate sending HTTP requests and processing the returned data. So you can use them.

Anyway, the above API call, you can use PHP's Curl library to call, this is relatively simple, and then encapsulate one, basic. Do not understand this, suitable for beginners PHP curl.

Of course you can also use a third-party encapsulated PHP http client (in fact it still uses PHP's Curl library or Fsockopen library), such as these (source: Recommended!). PHP Resources compiled by foreign programmers):
1. guzzle-A comprehensive HTTP client
2. Buzz-Another HTTP client
3. Requests-a simple HTTP library
4. Httpful-a chained HTTP library

There are also some plugins based on browser development, debugging Rest API is very convenient, provide several (chrome applies):
1. DHC (aka Dev HTTP Client) (your favorite, easy-to-use)
2. Postman
3. Advanced Rest Client

If you want to know about rest here restful-api-design-references collected some information, for the API caller, understand should be possible.

You can refer to the blog before Ruan Yi Peak to understand some of the conceptualization of things

Http://www.ruanyifeng.com/blog/2011/09/restful.html
Http://www.ruanyifeng.com/blog/2014/05/restful_api.html

Can be more reference under the Github.com API feel very beautiful

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.