Home > PHP Framework > ThinkPHP > body text

How thinkPHP uses curl to retrieve remote data

藏色散人
Release: 2021-06-28 09:31:11
forward
3172 people have browsed it

1: curl extension download:

composer require curl/curl
Copy after login

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();
Copy after login

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();
Copy after login

The return parameters are:

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

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();
Copy after login

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//获取响应头信息
Copy after login

《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!

Related labels:
source:segmentfault.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template