search
Homephp教程php手册php中curl使用指南

这篇文章主要介绍了php中curl使用指南,十分详细,需要的朋友可以参考下

许多同学在第一次使用curl的时候感觉一个头两个大(包括我在内),看着这一条条的curl_setopt函数完全摸不着头脑,不过在你花10分钟看了我的介绍后相信你以后也能轻松戏耍php的curl了

首先,请看一个curl代码(花10秒钟,,略看一遍,然后跳到后文)

复制代码 代码如下:


$data = "[...]";
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "https://example.com/path/for/soap/url/");
curl_setopt($tuCurl, CURLOPT_PORT , 443);
curl_setopt($tuCurl, CURLOPT_VERBOSE, 0);
curl_setopt($tuCurl, CURLOPT_HEADER, 0);
curl_setopt($tuCurl, CURLOPT_SSLVERSION, 3);
curl_setopt($tuCurl, CURLOPT_SSLCERT, getcwd() . "/client.pem");
curl_setopt($tuCurl, CURLOPT_SSLKEY, getcwd() . "/keyout.pem");
curl_setopt($tuCurl, CURLOPT_CAINFO, getcwd() . "/ca.pem");
curl_setopt($tuCurl, CURLOPT_POST, 1);
curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".strlen($data)));
$tuData = curl_exec($tuCurl);
if(!curl_errno($tuCurl)){
  $info = curl_getinfo($tuCurl);
  echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
} else {
  echo 'Curl error: ' . curl_error($tuCurl);
}
curl_close($tuCurl);
echo $tuData;
?>

WTF,这到底是在做什么?

想要学会这种“高端”的用法吗?

首先,相信你肯定知道网址大部分是由http开头的,那是因为他们需用通过http(超文本传送协议 HTTP-Hypertext transfer protocol)来进行数据传输,但是传输数据不是简单的将一句"Hello"传到服务器上就搞定的事情,发送者为了方便接受者理解发送者的实际意图以及知道发送人到底是何许人也,发送者往往要将许多额外信息一并发给接受者,就像寄信人需要在信件外套一个信封一样,信封上写着各种发信人的信息。所有的这些最终合并成了一个叫做报文(message)的玩意,也就构成了整个互联网的基础。

php中curl使用指南

curl的工作就是通过http协议发送这些message (php的libcurl目前还支持https、ftp、telnet等其他协议)

现在再看代码,实际上代码只做了五件事情

curl_init()初始化curl
curl_setopt()设置传输数据和参数
curl_exec()执行传输并获取返回数据
curl_errono()返回错误码
curl_close()关闭curl
下面给出使用GET和POST方法如何抓取和提交任意页面的数据

复制代码 代码如下:


    //初始化
    $curl = curl_init();
    //设置url
    curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com');
    //设置返回获取的输出为文本流
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //显示获得的数据
    print_r($data);
?>
    //初始化
    $curl = curl_init();
    //设置url
    curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com');
    //设置返回获取的输出为文本流
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    //设置post数据
    curl_setopt($curl, CURLOPT_POSTFIELDS, array("data"=>"value");
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //打印数据
    print_r($data);
?>

感兴趣的同学还可以参考php官方文档,学习更多curl用法

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.