分类 PHP代码 下的文章

打开 opcache配置文件:

nano /etc/php/8.2/fpm/conf.d/10-opcache.ini

 修改内容为:

; configuration for php opcache module
; priority=10
zend_extension=opcache.so
opcache.jit=1255
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.jit_buffer_size=128M

保存后重启php-fpm:

systemctl restart php8.2-fpm

 

//?? 等价于 isset
$a = '';
$b = isset($a) ? $a : 'hahha';
$b = $a ?? 'hahha';
//$b is ''
//适用于多层级array的属性读取
$arr = [ 'son'=>['name'=>'child'] ];
echo $arr['son']['age'] ?? '19';
//output: 19
echo $arr['non']['name'] ?? 'non child';
//output: non child

//?: 等价于empty
$c = empty($a) ? 'hahaha' : $a;
$c = $a ?: 'hahaha';
//c is 'hahaha'

 

在PHP中使用curl进行HTTP请求,`$usePost`设置为true可以使用post请求。

<!--?php
header("Content-type: text/html; charset=utf-8"); //设置返回内容编码
/**
 * 模拟HTTP请求
 * @param string $url
 * @param array $post_data
 */
function request($url = '',$post_data = array(),$usePost=false) {
    if (empty($url) || empty($post_data)) {
        return false;
    }
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$url);//设置网址
    curl_setopt($ch, CURLOPT_HEADER, 0);//不要返回的Header区域内容
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串
    curl_setopt($ch, CURLOPT_POST, $usePost);//是否以post方式提交数据
    curl_setopt($ch, CURLOPT_ENCODING, "");//设置自动解压压缩的返回内容
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//设置提交的数据
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书下同
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return $data;
}

$url = 'https://some.domain/somepath';
$post_data = array('param1'=>'value1','param2'=>'value2');
$res = request($url, $post_data); 
echo $res;