1、curl介绍(需要开启curl扩展)
curl是PHP的一个扩展,利用该扩展可以实现服务器之间的数据或文件的传输,也就是curl就是一个工具,用来做服务器之间的数据,文件传输的工具。
2、应用场景
可以用来采集html网页文件,其他服务器提供的接口数据等
3、curl简单使用
<?php header('content-type: text/html; charset=utf8'); ini_set('display_errors', true); $curl = curl_init(); //初始化,并保存句柄 curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com'); //配置选项 curl_exec($curl); //执行请求 curl_close($curl); //关闭curl ?>
进行post请求
<?php header('content-type: text/html; charset=utf8'); ini_set('display_errors', true); $curl = curl_init(); //初始化,并保存句柄 curl_setopt($curl, CURLOPT_POST,true); //进行post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, ['txt' => 'are you ok???']); //进行post传参 curl_setopt($curl, CURLOPT_URL, 'http://localhost/learn/test.php'); //配置选项 curl_exec($curl); //执行请求 curl_close($curl); //关闭curl ?>
注意:如果发送的请求的网站是没有https安全协议的情况下,可以执行跳过安全协议较验证,可以用以下两个配置项
<?php use Curl\Curl; header('content-type: text/html; charset=utf8'); ini_set('display_errors', true); require_once('./vendor/autoload.php'); $curl = curl_init(); //跳过验证https安全协议 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_URL, 'https://www.baidu.com'); curl_exec($curl); curl_close($curl); ?>
4、启用curl比较成熟的扩展插件(具体可参看https://packagist.org/packages/php-curl-class/php-curl-class)
<?php use Curl\Curl; header('content-type: text/html; charset=utf8'); ini_set('display_errors', true); require_once('./vendor/autoload.php'); $curl = new Curl(); //$curl->setOpt(CURLOPT_SSL_VERIFYHOST, false); //$curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); $curl->setOpts([CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false]); $curl->get('https://www.baidu.com'); if($curl->error) { echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n"; } else { echo '<pre>'; echo $curl->response; } ?>
如果需要传递参数
// https://www.example.com/search?q=keyword $curl = new Curl(); $curl->get('https://www.example.com/search', array( 'q' => 'keyword', ));
如果需要设置请求头
<?php use Curl\Curl; header('content-type: text/html; charset=utf8'); ini_set('display_errors', true); require_once('./vendor/autoload.php'); $curl = new Curl(); $curl->setOpts([CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false]); $curl->setHeader('Token','46c48bec0d282018b9d167eef7711b2c'); $curl->get('http://*********.php'); if($curl->error) { echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n"; } else { $arr = json_decode(json_encode($curl->response), true); var_dump($arr); } ?>
其他设置
$curl->setBasicAuthentication('username', 'password'); $curl->setUserAgent('MyUserAgent/0.0.1 (+https://www.example.com/bot.html)'); $curl->setReferrer('https://www.example.com/url?url=https%3A%2F%2Fwww.example.com%2F'); $curl->setHeader('X-Requested-With', 'XMLHttpRequest'); $curl->setCookie('key', 'value');
post请求
$curl = new Curl(); $curl->post('https://www.example.com/login/', array( 'username' => 'myusername', 'password' => 'mypassword', ));
put请求
$curl = new Curl(); $curl->put('https://api.example.com/user/', array( 'first_name' => 'Zach', 'last_name' => 'Borboa', ));
delete请求
$curl = new Curl(); $curl->delete('https://api.example.com/user/', array( 'id' => '1234', ));
下载文件
// Case-insensitive access to headers. $curl = new Curl(); $curl->download('https://www.example.com/image.png', '/tmp/myimage.png'); echo $curl->responseHeaders['Content-Type'] . "\n"; // image/png echo $curl->responseHeaders['CoNTeNT-TyPE'] . "\n"; // image/png
句柄关闭
// Manual clean up. $curl->close();
利用curl进行上传文件
<?php use Curl\Curl; header('content-type: text/html; charset=utf8'); ini_set('display_errors', true); require_once('./vendor/autoload.php'); $curl = new Curl(); $curl->setOpts([CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false]); $curl->post('http://localhost/learn/test.php', ['file' => new CURLFile('C:\wamp64\www\learn\file\other.png')]); //也可以用new CURLFile(realpath('./file/other.png'))
if($curl->error) {
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
} else {
$arr = json_decode(json_encode($curl->response), true); var_dump(json_decode($arr, true));
} ?>
模拟接收
<?php header('content-type: text/html; charset=utf8'); class Test { public function __construct() {} public function file() { $file = $_FILES; echo json_encode($file); } } (new Test())->file(); ?>