Table of Contents
Problem
Solution process
Home Backend Development PHP Tutorial Introduction to the method of PHP supporting CURL string certificate transmission (code)

Introduction to the method of PHP supporting CURL string certificate transmission (code)

Mar 25, 2019 am 09:23 AM
php

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

<?php

    public function getTmpPathByContent($content)

    {

        $tmpFile = tmpfile();

        fwrite($tmpFile, $content);

        $tempPemPath = stream_get_meta_data($tmpFile);

        return $tempPemPath[&#39;uri&#39;];  ///tmp/phpXZCtAO

    }

?>

Copy after login

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

file_get_contents(/tmp/phpyyiOZv): failed to open stream: No such file or directory

Copy after login

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

<?php

    public function getTmpPathByContent($content)

    {

        static $tmpFile = null;

        $tmpFile = tmpfile();

        fwrite($tmpFile, $content);

        $tempPemPath = stream_get_meta_data($tmpFile);

        return $tempPemPath[&#39;uri&#39;];

    }

?>

Copy after login

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

-----BEGIN CERTIFICATE-----

MIIEbDCCA9WgAwIBAgIEAWJKHDANBgkqhkiG9w0BAQUFADCBijELMAkGA1UEBhMC

Q04xEjAQBgNVBAgTCUd1YW5nZG9uZzERMA8GA1UEBxMIU2hlbnpoZW4xEDAOBgNV

BAoTB1RlbmNlbnQxDDAKBgNVBAsTA1dYRzETMBEGA1UEAxMKTW1wYXltY2hDQTEf

MB0GCSqGSIb3DQEJARYQbW1wYXltY2hAdGVuY2VudDAeFw0xNzA4MDcwOTIxNDda

Fw0yNzA4MDUwOTIxNDdaMIGbMQswCQYDVQQGEwJDTjESMBAGA1UECBMJR3Vhbmdk

b25nMREwDwYDVQQHEwhTaGVuemhlbjEQMA4GA1UEChMHVGVuY2VudDEOMAwGA1UE

CxMFTU1QYXkxMDAuBgNVBAMUJ+a3seWcs+W4guaYjua6kOi9r+S7tuiCoeS7veac

iemZkOWFrOWPuDERMA8GA1UEBBMIMTAyNTkyODEwggEiMA0GCSqGSIb3DQEBAQUA

A4IBDwAwggEKAoIBAQDg2D3++uOxY/yMGQPBnROvyYimnCsfGE0dnqdGUTCykqBh

yfv82zE1/St/4DQX2QDiIvLif+sMGcYwF4bkzdY+HgitYLI0k5o/5LCNZOMctuio

kdYC2bNdWHq2y9S5UWLQR1Zvq+6QyPBVBVY9yq9xtQhIlUTsZnICAp3iQLfQUR3l

aEdH9IERoRUIkbyb8oX5ONQz4P9jOeE9C5iwx0QrH4s01NFhkhr8JHlugRLpo9vA

xGgi/48fOlONj6wWal5Gt0OvvEbIwgQwya15KBX2YeGnZvYBQa+lQMeXEqZSFie3

G+wGvbtlONczQEtp+JDxLZLUS/FT7U0TQN/t8JDvAgMBAAGjggFGMIIBQjAJBgNV

HRMEAjAAMCwGCWCGSAGG+EIBDQQfFh0iQ0VTLUNBIEdlbmVyYXRlIENlcnRpZmlj

YXRlIjAdBgNVHQ4EFgQUjDJ75bu3Roog7XOH6uFAdZ6kpcIwgb8GA1UdIwSBtzCB

tIAUPgUm9iJitBVbiM1kfrDUYqflhnShgZCkgY0wgYoxCzAJBgNVBAYTAkNOMRIw

EAYDVQQIEwlHdWFuZ2RvbmcxETAPBgNVBAcTCFNoZW56aGVuMRAwDgYDVQQKEwdU

ZW5jZW50MQwwCgYDVQQLEwNXWEcxEzARBgNVBAMTCk1tcGF5bWNoQ0ExHzAdBgkq

hkiG9w0BCQEWEG1tcGF5bWNoQHRlbmNlbnSCCQC7VJcrvADoVzAOBgNVHQ8BAf8E

BAMCBsAwFgYDVR0lAQH/BAwwCgYIKwYBBQUHAwIwDQYJKoZIhvcNAQEFBQADgYEA

ucJLJkkHxlqQCEapZOWmySutqNVZxFbqyG//UXxxpA/1yG4e+KmufKZWv+c+MtYI

8i0KDDCv/UE+kkFIrHYDDKsdLRpxrYOUHGoqq0c7yBJ6Dimgy6m8U8FsEv3HtUR2

8g5xrg2Tc5MPWEp9ncEw575hGk0CXLDGOkI1nU+pGqk=

-----END CERTIFICATE-----

Copy after login

Now you can set the generated temporary file address to CURLOPT_SSLCERT

1

2

3

4

5

<?php

    $sslCertPath = getTmpPathByContent($content);

    curl_setopt($ch,CURLOPT_SSLCERT, $sslCertPath);

    //......

?>

Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles