


Introduction to the method of PHP supporting CURL string certificate transmission (code)
This article brings you an introduction to the method (code) for PHP to support CURL string certificate transmission. It has certain reference value. Friends in need can refer to it. I hope it will help You helped.
Recently, when connecting to WeChat payment, a certificate needs to be used at the refund point. Since we are a SAAS platform, we need to support multi-party and multi-channel payments. If we save all certificate files in the application server, it will be affected by SLB. , will cause the files on a certain machine to be out of sync and hinder the refund process. However, if the files are stored in OSS, the backend must download them from OSS to the application server to ensure consistency. After much deliberation, we finally decided to save the certificate content in the database. Different customers correspond to one certificate file. No matter how many machines are used in the cluster, the consistency of the file can be ensured and redundant downloading steps can be avoided.
Problem
But I encountered a problem just after I did it. PHP's CURL certificate does not support the transmission of strings, and can only fill in the certificate path (the following is the official statement)
Client certificates must be specified by a path expression to a certificate store.
Solution process
My first thought is to create a blank file and write the certificate content Go in, wait for the certificate to be used, and then delete the file. However, the operation of creating an entity file and then deleting it consumes performance and is very troublesome. Is there a way to create a temporary file? Yes, the tmpfile()
function can help us create a temporary file and get the file path, so I wrote a method to get the temporary file path
1 2 3 4 5 6 7 8 9 |
|
What is sad is that through this The content of the path returned by the method cannot be read at all. I even thought that I had been cheated.
1 |
|
I found the reason after reading the official documentation. Iftmpfile()
the handle reference count returned is 0 If so, the temporary file will be recycled, and the temporary path will naturally become invalid. Obviously, after the method getTmpPathByContent()
is executed, the life cycle of the local variable $tmpFile
will end (official document As follows)
The file is automatically removed when closed (for example, by calling fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends.
After confirming the root cause, we now urgently need to find a variable type whose life cycle terminates when the process ends to save the handle. What type can meet the conditions? static variables. The difference between static variables and local variables is that memory space is allocated for them at the beginning of the PHP life cycle and stored in the global variable area. Global variables are destroyed during the module shutdown phase. In this case, declare static variables You can make $tmpFile
the reference count continue to remain greater than 0, then our code can do the following processing
1 2 3 4 5 6 7 8 9 10 |
|
Execute it again and successfully read the contents of the temporary file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Now you can set the generated temporary file address to CURLOPT_SSLCERT
1 2 3 4 5 |
|
This article is all over here, you can pay attention to more other exciting content The php video tutorial column of the PHP Chinese website!
The above is the detailed content of Introduction to the method of PHP supporting CURL string certificate transmission (code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
