Use RESTfulAPI in PHP

Source: Internet
Author: User
For example, I need to upload an image to an independent image server. For details about restful API, refer to github. comalibabanginx-tfsblobmasterTFS_RESTful_API.markd... syntax POSTv1appkeyHTTP1 .... how to Use RESTful APIs in PHP
For example, I need to upload an image to an independent image server.

Restful api reference
Https://github.com/alibaba/nginx-tfs/blob/master/TFS_RESTful_API.markd...
Syntax

POST, v1, and 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, 30 Nov 2012 03:05:00 GMT

[Data]

How should I write PHP? Is there any reference?

Reply content:

How to Use RESTful APIs in PHP
For example, I need to upload an image to an independent image server.

Restful api reference
Https://github.com/alibaba/nginx-tfs/blob/master/TFS_RESTful_API.markd...
Syntax

POST, v1, and 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, 30 Nov 2012 03:05:00 GMT

[Data]

How should I write PHP? Is there any reference?

Because RESTful APIs are based on HTTP, as long as you have an HTTP client (such as a browser, simulating with the PHP curl Library), you can communicate with the API.

What you see above is some HTTP header information and data when the client communicates with the API Based on the HTTP protocol. For example, if you openDeveloper ToolsAnd then selectNetwork)You can see all the HTTP-related information of this communication.

Before calling an API, it is recommended that you first learn about HTTP and REST. This is very important.

The simple principle is as follows:Since the client (your application) must use the HTTP protocol to communicate with the API, it is easy. You just need to send an HTTP request to the API. Why? How to send? Just use the browser. Of course, it is convenient to use some plug-ins (as mentioned later ). The key is to use PHP to send HTTP requests and process requests. In that line, PHP provides the curl library and fsockopen, both of which can simulate HTTP requests and process returned data. So you can use them.

To put it bluntly, you can use the above API callPHP curl LibraryIt is relatively simple to call. You can encapsulate it by yourself. This is suitable for PHP curl beginners.

Of course, you can also use third-party encapsulatedPhp http client(In fact, it still uses the php curl library or fsockopen Library), such as these (source: Recommended! PHP resources compiled by programmers abroad ):
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 plug-ins developed based on browsers. It is very convenient to debug the rest api and provides several (applicable to chrome ):
1. DHC (aka Dev HTTP Client) (your favorite, easy to use)
2. Postman
3. Advanced Rest Client

If you want to know about RESTRestful-api-design-referencesI have collected some information. For API callers, I should know about it.

You can refer to the blog post Before Ruan Yifeng to learn some conceptual things.

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

For more information, see the github.com api.

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.