自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP語言中php curl的幾種應(yīng)用方式

開發(fā) 后端
今天向大家介紹的是在PHP中php curl的幾種應(yīng)用方式,包括:php curl的默認(rèn)調(diào)用方法,get方式訪問url;設(shè)置http header支持php curl訪問lighttpd服務(wù)器;設(shè)置curl,只獲取http header,不獲取body等等。

我們通過對(duì)PHP開發(fā)基礎(chǔ)入門這一專題的學(xué)習(xí)可以了解到,PHP語言的一些具體概念的實(shí)際應(yīng)用。今天我們向大家介紹的是在PHP中的php curl的幾種使用方式,希望對(duì)有需要的朋友有所幫助。

#t#1. php curl的默認(rèn)調(diào)用方法,get方式訪問url

  1. ....     
  2.     $ch = curl_init();     
  3.     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//設(shè)置http頭     
  4.     curl_setopt($ch, CURLOPT_ENCODING, "gzip" ); 
    //設(shè)置為客戶端支持gzip壓縮     
  5.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30 ); 
    //設(shè)置連接等待時(shí)間     
  6.     curl_setopt($ch, CURLOPT_URL, $url );     
  7.     curl_exec( $ch );     
  8.     if ($error = curl_error($ch) ) {     
  9.         //出錯(cuò)處理     
  10.         return -1;     
  11.     }     
  12.     fclose($fp);       
  13.     
  14.     $curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    //獲取http返回值     
  15.     if( $curl_code == 200 ) {     
  16.         //正常訪問url     
  17.     }     
  18.     //異常     
  19. ....    

2. 設(shè)置http header支持php curl訪問lighttpd服務(wù)器

  1. $header[]= 'Expect:';  

3. 設(shè)置curl,只獲取http header,不獲取body:

  1. curl_setopt($ch, CURLOPT_HEADER, 1);       
  2. curl_setopt($ch, CURLOPT_NOBODY, 1);      

或者只獲取body:

  1. curl_setopt($ch, CURLOPT_HEADER, 0);   
    // make sure we get the body     
  2. curl_setopt($ch, CURLOPT_NOBODY, 0);     

4. 訪問虛擬主機(jī),需設(shè)置Host

  1. $header[]= 'Host: '.$host;   

5. 使用post, put, delete等REStful方式訪問url

  1. post:   
  2.  
  3.   curl_setopt($ch, CURLOPT_POST, 1 );   
  4.  
  5. put, delete:   
  6.  
  7.   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");  
    //或者PUT,需要服務(wù)器支持這些方法。  

6. php curl保存下載內(nèi)容為文件

  1. curl_setopt($ch, CURLOPT_FILE, $fp);  
責(zé)任編輯:曹凱 來源: javaeye社區(qū)
相關(guān)推薦

2019-08-28 09:04:02

Go語言Python操作系統(tǒng)

2011-05-25 13:22:05

PHPJSON

2009-11-23 11:03:12

php_curl庫

2011-05-19 14:00:51

PHP單引號(hào)雙引號(hào)

2011-05-13 16:30:25

PHP

2009-12-25 15:02:01

php擴(kuò)展cURL

2009-11-30 16:24:24

PHP腳本

2011-05-30 13:15:05

PHP

2009-12-02 18:03:00

PHP cURL

2009-11-25 14:31:43

PHP自然語言倒序

2009-01-15 13:26:14

PHPWeb開發(fā)ASP.NET

2016-08-11 09:48:53

2009-11-26 14:23:10

PHP正則模式修正符

2021-04-28 09:02:48

Golang語言Context

2009-12-10 17:02:50

PHP站點(diǎn)性能

2009-09-09 11:24:46

PHP實(shí)現(xiàn)MVC

2011-05-20 10:37:04

PHP

2011-05-07 08:35:22

PHP

2009-12-02 10:01:04

PHP亂碼問題

2009-12-02 20:02:18

PHP實(shí)現(xiàn)頁面跳轉(zhuǎn)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)