## CURL實現GET和POST
①:GET方式實現
```
<?php
//初始化
$curl = curl_init ( ) ;
//設置抓取的url
curl_setopt ( $curl , CURLOPT_URL , 'http://www.baidu.com' ) ;
//設置頭文件的信息作為數據流輸出
curl_setopt ( $curl , CURLOPT_HEADER , 1 ) ;
//設置獲取的信息以文件流的形式返回,而不是直接輸出。
curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , 1 ) ;
//執行命令
$data = curl_exec ( $curl ) ;
//關閉URL請求
curl_close ( $curl ) ;
//顯示獲得的數據
print_r ( $data ) ;
?>
```
②:POST方式實現
```
<?php
//初始化
$curl = curl_init ( ) ;
//設置抓取的url
curl_setopt ( $curl , CURLOPT_URL , 'http://www.baidu.com' ) ;
//設置頭文件的信息作為數據流輸出
curl_setopt ( $curl , CURLOPT_HEADER , 1 ) ;
//設置獲取的信息以文件流的形式返回,而不是直接輸出。
curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , 1 ) ;
//設置post方式提交
curl_setopt ( $curl , CURLOPT_POST , 1 ) ;
//設置post數據
$post_data = array (
"username" => "coder" ,
"password" => "12345"
) ;
curl_setopt ( $curl , CURLOPT_POSTFIELDS , $post_data ) ;
//執行命令
$data = curl_exec ( $curl ) ;
//關閉URL請求
curl_close ( $curl ) ;
//顯示獲得的數據
print_r ( $data ) ;
?>
```
③:如果獲得的數據時json格式的,使用json_decode函數解釋成數組。
```
$output_array = json_decode($output,true);
```
如果使用json_decode($output)解析的話,將會得到object類型的數據。