200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > php curl实现get请求 PHP中使用curl实现Get和Post请求 | 严佳冬

php curl实现get请求 PHP中使用curl实现Get和Post请求 | 严佳冬

时间:2019-01-17 21:03:42

相关推荐

php curl实现get请求 PHP中使用curl实现Get和Post请求 | 严佳冬

一、基本结构

1、初始化

curl_init()

2、设置变量

curl_setopt() 。最为重要,一切玄妙均在此。有一长串curl参数可供设置,它们能指定URL请求的各个细节。

执行并获取结果

3、执行

curl_exec()

4、释放cURL句柄

curl_close()

二、Get方式实现

//初始化

$ch = curl_init();

//设置选项,包括URL

$url = "xxx";

$timeout = 10;

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_HEADER, 0);

//执行并获取HTML文档内容

$output = curl_exec($ch);

//释放curl句柄

curl_close($ch);

//打印获得的数据

print_r($output);

三、Post方式实现

$url = "http://localhost/web_services.php";

$post_data = array ("username" => "bob","key" => "12345");

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// post数据

curl_setopt($ch, CURLOPT_POST, 1);

// post的变量

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$output = curl_exec($ch);

curl_close($ch);

//打印获得的数据

print_r($output);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。