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);
總結
以上是生活随笔為你收集整理的php curl实现get请求,PHP中使用curl实现Get和Post请求 | 严佳冬的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蔚来汽车全面走出国门 2025年前进入2
- 下一篇: 消息人士称骁龙8 Gen 2已在台积电4