PHP利用curl实现get与POST提交数据
curl实现get提交数据,代码如下:
-
- $curl = curl_init();
-
- curl_setopt($curl, CURLOPT_URL, 'http://www.phpfensi.com');
-
- curl_setopt($curl, CURLOPT_HEADER, 0);
-
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- "User-Agent: {'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729)'}",
- "Accept-Language: {en-us,en;q=0.5}"
- ));
-
- curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
-
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
- curl_setopt($ch, CURLOPT_NOBODY, 1);
-
- $data = curl_exec($curl);
-
- curl_close($curl);
-
-
- if ($output === FALSE) {
- echo "cURL Error: " . curl_error($ch);
- }
-
- $info = curl_getinfo($ch);
- echo '获取'. $info['url'] . '耗时'. $info['total_time'] . '秒';
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
curl实现POST提交数据,http的post实现,代码如下:
-
- extract($_POST) ;
-
- $url = 'http://www.phpfensi.com' ;
- $fields = array(
- 'lname'=>urlencode($last_name) ,
- 'fname'=>urlencode($first_name) ,
- 'title'=>urlencode($title) ,
- 'company'=>urlencode($institution) ,
- 'age'=>urlencode($age) ,
- 'email'=>urlencode($email) ,
- 'phone'=>urlencode($phone)
- );
-
- foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; }
- rtrim($fields_string ,'&') ;
-
- $ch = curl_init() ;
-
- curl_setopt($ch, CURLOPT_URL,$url) ;
- curl_setopt($ch, CURLOPT_POST,count($fields)) ;
- curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ;
-
- $result = curl_exec($ch) ;
-
- curl_close($ch) ;