PHP

掌握PHP中的CURL请求技巧和教程

silverwq
2022-05-28 / 0 评论 / 233 阅读 / 正在检测是否收录...

一、概述

本篇文章,介绍php curl 的几个常用方式,包括post请求,上传文件等。

二、代码

  1. post 请求
    
    $url = 'www.baidu.com';
    $post_data = [];

$curl_handle = curl_init($url);//初始化curl
curl_setopt($curl_handle, CURLOPT_URL, $url);//指定curl地址
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_POST, true);//post请求
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post_data);//post数据
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);//https请求
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);//https请求
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 15);//超时
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10);
$data = curl_exec($curl_handle);// 返回

print_r($data);

1. 上传文件
```bash
$objectFile = new \CURLFile('./fileName.pdf');
$post_data = [
    'object_file' => $object_file,//这是要上传的文件 php 大于 5
    'appkey' => "test",
    'timestamp' => time(),
    'title' => urlencode($title),
    'flag' => 0,
    'summary' => urlencode('测试文档'),
];
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post_data);
$data = curl_exec($curl_handle);
0

评论 (0)

取消