Home  >  Article  >  PHP Framework  >  How thinkPHP uses curl to retrieve remote data

How thinkPHP uses curl to retrieve remote data

藏色散人
藏色散人forward
2021-06-27 16:48:123160browse

1: curl extension download:

composer require curl/curl

curl/curl address: https://packagist .org/package...

2: Use curl to implement get request

1: The get parameter is directly spliced ​​on the url

$curl = new Curl();
$curl->get('https://api.wj0511.com/v1/literary/detail?token=XXX&id=374');
$curl->close();

2: The get parameter passes through the second Parameters are passed

$curl = new Curl();
$curl->get('https://api.wj0511.com/v1/literary/detail',array(
    'token' => 'XXX',
    'id' => 374
));
$curl->close();

The return parameters are:

$curl->error//是否错误
$curl->error_code//错误编码
$curl->response//返回数据

Two: Use curl to implement post request

$params = array(
    'username' => 'test',
    'password' => 'test',
);
$curl = new Curl();
$curl->post('https://api.wj0511.com/v1/login/login', $params);
$curl->close();

The return parameters are the same as the get request

Three: curl Some common methods of /curl expansion:

$curl->setBasicAuthentication('username', 'password');//设置基本身份验证
$curl->setHeader('X-Requested-With', 'XMLHttpRequest');//设置请求头信息
$curl->setCookie('key', 'value');//设置cookie
$curl->request_headers//获取请求头信息
$curl->response_headers//获取响应头信息

《Related recommendations: The latest 10 thinkphp video tutorials

The above is the detailed content of How thinkPHP uses curl to retrieve remote data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete