search
Homephp教程php手册php 中使用cURL发送get/post请求,上传图片,批处理

cURL是利用url语法规定传输文件和数据的工具。php中有curl拓展,一般用来实现网络抓取,模拟发送get post请求,文件上传。 在php中建立curl的基本步骤如下: 1 初始化 2 设置选项,包括url 3 执行并获取结果 4 释放curl句柄。 在工作和学习中,我也是时常用

  cURL是利用url语法规定传输文件和数据的工具。php中有curl拓展,一般用来实现网络抓取,模拟发送get   post请求,文件上传。

  在php中建立curl的基本步骤如下:

  1 初始化        2 设置选项,包括url        3  执行并获取结果     4 释放curl句柄。

  在工作和学习中,我也是时常用的curl。由于在使用curl设置选项时,各种选项比较难以记忆,需要参考,故在此记录下常用的一些例子,以便后来参考。

实例一 : 抓取网页数据(以拉手网开放api为例,也是get请求)

<?php header("Content-type: text/html; charset=utf-8");  
$ch = curl_init();//初始化

/*============开始设置curl各种选项================*/
curl_setopt($ch, CURLOPT_URL, "http://open.lashou.com/opendeals/lashou/city.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


$html = curl_exec($ch);//执行句柄,获取返回内容

curl_close($ch);//释放句柄

echo $html
?>

  如果用这种方法发get请求,参数附加到url后面即可

     如curl_setopt($ch, CURLOPT_URL, "http://localhost/tqj/date/p822.php?name=yyyyy");

实例 2  利用curl发送post请求

<?php $uri = "http://localhost/tqj/date/p822.php";

// post参数数组
$data = array (
        'name' => 'tianquanjun', 
 		'password' => 'tianquanjun',
);

//初始化 
$ch = curl_init ();

//各种项设置,网上参考而来,可以查看php手册,自己设置
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

//执行
$return = curl_exec ( $ch );
//释放
curl_close ( $ch );
 
print_r($return);
?>

  

实例三  :curl 过程调试与错误信息处理

<?php $uri = "http://localhost/tqj/date/p822.php";

// post参数数组
$data = array (
        'name' => 'tianquanjun', 
 		'password' => 'tianquanjun',
);

//初始化 
$ch = curl_init ();

//各种项设置,网上参考而来,可以查看php手册,自己设置
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

//执行
$return = curl_exec ( $ch );

<span>//容错机制
if($return === false){
	var_dump(curl_error($ch));
	}

//curl_getinfo()获取各种运行中信息,便于调试	
$info = curl_getinfo($ch);

echo "执行时间".$info['total_time'].PHP_EOL;</span>

//释放
curl_close ( $ch );
 
print_r($return);
?>

  其中利用curl_error()获取错误信息,curl_getinfo()获取运行相关信息。

 

实例四  上传图片,获取返回信息。跨域上传图片,同时获取返回信息,这个就能大显身手。和post比较像,注意文件之前加一个@符号

<?php $uri = "http://localhost/tqj/date/p822.php";

// post参数数组
$data = array (
        'author' => 'tianquanjun', 
 		'upload' => '@C:\Users\tianquanjun.DANGDANG\Pictures\a.jpg',
);

//初始化 
$ch = curl_init ();

//各种项设置,网上参考而来,可以查看php手册,自己设置
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

//执行
$return = curl_exec ( $ch );

//容错机制
if($return === false){
	var_dump(curl_error($ch));
	}

//curl_getinfo()获取各种运行中信息,便于调试	
$info = curl_getinfo($ch);

echo "执行时间".$info['total_time'].PHP_EOL;

//释放
curl_close ( $ch );
 
print_r($return);
?>

 

实例五 : curl批处理。

  curl有一个高级特性,批处理句柄。允许打开多个curl链接。 

  批处理就是打开多个curl句柄,并把这些句柄指派给一个批处理句柄,然后在while循环里等待处理完毕。curl_multi_exec()算是称得上多线程处理,不过它还是属于异步的范畴。

 

<?php header("Content-type: text/html; charset=gbk"); 
$urls=array('http://www.baidu.com','http://www.qq.com/');
$ch=array();
//批处理句柄
$mh=curl_multi_init();

//打开多个curl句柄,并指派给一个批处理句柄
$ch[0]=curl_init($urls[0]);
$ch[1]=curl_init($urls[1]);
for($i=0;$i<2;$i++)
{
curl_setopt($ch[$i],CURLOPT_RETURNTRANSFER,1);
curl_multi_add_handle($mh,$ch[$i]);
 
}
$running = NULL;
do{
    usleep(10000);
    curl_multi_exec($mh,$running);//实现批处理,可以看做curl多线程,实际是异步范畴
}while($running>0);
$res=array();
for($j=0;$j

  基本算是列举了常用的一些实例。要想灵活运用curl,还是得熟悉curl的各个设置项,这些设置项才是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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

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.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)