This article mainly introduces the request signature required to create a COS interface using PHP. It is compared with the examples given in the official documents to verify the correctness of the algorithm. Friends in need can refer to it
What is COS and request signature
COS is the abbreviation and abbreviation of Tencent Cloud Object Storage. The request signature is created by a specific algorithm and needs to be provided by a third party on demand when calling COS related interfaces. A set of string information that will uniquely identify the current third-party identity and provide identification of both communicating parties. Only valid signed COS will provide services
Goal
Use PHP to create the request signature required for the COS interface, compare it with the example given in the official document, and verify the correctness of the algorithm
Understand the request signature
Come first Look at the request signature given in an official document
Summary of the characteristics of the request signature
is a string of characters
key=value key-value pair format, key is a fixed value
There are 7 pairs of key=value
sha1 is also a parameter, but As of the official release, only sha1 is supported, so you can directly assign values
SignedHeaderList, SignedParameterList, and Signature three values need to be generated through algorithms
key values For a detailed description, see the official documentation.
Breakdown one by one
Requesting a signature requires a total of 7 values. Let’s explain one by one below and break each one
q-sign-algorithm
Signature algorithm, official Currently only sha1 is supported, so just give the value directly
q-ak
The account ID, which is the user's SecretId, can be obtained on the console Cloud API Key page
q-sign-time
The valid start and end time of the current signature, Unix timestamp format, English half-width semicolon; separated, format such as 1480932292;1481012298
q-key-time
Same as q-sign-time value
q-header-list
Personal understanding, it consists of HTTP request headers, take all or part of the request headers, and change the request in the form of key:value The key part of the item is taken out, converted to lower case, multiple keys are sorted according to the dictionary, and connected with the characters ; to finally form a string
key is Host and Content-Type, output after calculation content-type;host
q-url-param-list
Personal understanding, it consists of HTTP request parameters, take all or part of the request parameters, and key The key part of the request parameter in the form of =value is taken out, converted to lowercase, multiple keys are sorted according to the dictionary, connected with the characters ;, and finally formed into a string
For example, the original HTTP request is:
GET /?prefix=abc&max-keys=20
Copy after login
key It is prefix and max-keys. After operation, max-keys; prefix is output. If the request has no parameters such as put and post, it will be empty.
q-signature
Calculated based on HTTP content Signature, algorithm is provided by COS, just give the value as required
Official example and reference result
Before starting to write the logic, first take a look at the reference value given by the official example, and the calculated The final result can be compared with the logic developed by yourself
The original HTTP request can also be understood as the HTTP request before calculating the signature or when no signature is required:
PUT /testfile2 HTTP/1.1
Host: bucket1-1254000000.cos.ap-beijing.myqcloud.com
x-cos-content-sha1: 7b502c3a1f48c8609ae212cdfb639dee39673f5e
x-cos-storage-class: standard
Hello world
Copy after login
After calculating the signature The HTTP request that should be obtained:
PUT /testfile2 HTTP/1.1
Host: bucket1-1254000000.cos.ap-beijing.myqcloud.com
x-cos-content-sha1: 7b502c3a1f48c8609ae212cdfb639dee39673f5e
x-cos-storage-class: standard
Authorization: q-sign-algorithm=sha1&q-ak=AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q&> q-sign-time=1417773892;1417853898&q-key-time=1417773892;1417853898&q-header-list=host;x-cos-content-sha1;x-cos-storage-class&q-url-param-list=&q-signature=14e6ebd7955b0c6da532151bf97045e2c5a64e10
Hello world
Copy after login
Conclusion: The algorithm is correct if it can get the string after Authorization
Preparation work
Let’s take a look (officially provided ) user information and HTTP information:
SecretId: AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q
SecretKey: BQYIM75p8x0iWVFSIgqEKwFprpRSVHlz
Signature valid start time: 1417773892
Signature valid stop time: 1417853898
HTTP original request header: It is not difficult to obtain according to the example in the previous section The HTTP original request has three contents: Host, x-cos-content-sha1 and x-cos-storage-class
HTTP request parameters: It is a PUT request, there is no ? parameter
Calculate signature
Put all the parameters in the preparation work into the request signature rules, and you can get the results easily, as shown in the following table:
Key(key)
Value(value)
Remarks
##q-sign-algorithm
sha1
Currently only supports sha1 signature algorithm
q-ak
AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q
SecretId field
q-sign-time
1417773892;1417853898
2014/12/5 18:04:52 to 2014/12/6 16:18: 18
##q-key-time
1417773892;1417853898
2014/12/5 18:04:52 to 2014/12/6 16 :18:18
q-header-list
host;x-cos-content-sha1;x-cos-storage-class
A lexicographically sorted list of HTTP header keys
The above is the detailed content of PHP generates the request signature required by Tencent Cloud COS interface. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase.
What is a vowel?
Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase:
a, e, i, o, u
Example 1
Input: String = "Tutorialspoint"
Output: 6
explain
The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total
Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.