curl客戶端筆記

curl客戶端筆記

1.cURL介紹

  cURL 是一個利用URL語法規定來傳輸文件和數據的工具,支持很多協議,如HTTP、FTP、TELNET等。最爽的是,PHP也支持 cURL 庫。

2.基本結構

  在學習更為複雜的功能之前,先來看一下在PHP中建立cURL請求的基本步驟:

  (1)初始化

     curl_init()

  (2)設置變數

    curl_setopt() 。最為重要,一切玄妙均在此。有一長串cURL參數可供設置,它們能指定URL請求的各個細節。要一次性全部看完並理解可能比較困難,所以今天我們只試一下那些更常用也更有用的選項。

  (3)執行並獲取結果

    curl_exec()

  (4)釋放cURL句柄

    curl_close()

3.cURL實現Get和Post

示例代碼:

<?php/** * Curl客戶端 * * @author WillMao */class CurlHelper{ public $ch; public $url=null; public function __construct(){ $this->ch = curl_init(); } public function setUrl($url){ if(!preg_match(!^w+://!i, $url)) { $url = http://.$url; } $this->url = $url; } public function setOptions($key,$value){ curl_setopt($this->ch, $key, $value); } public function setDefault($timeout=30,$header=false){ empty($timeout)?$this->setOptions(CURLOPT_TIMEOUT,30):$this->setOptions(CURLOPT_TIMEOUT,$timeout); $this->setOptions(CURLOPT_HEADER,$header); $this->setOptions(CURLOPT_RETURNTRANSFER,true); $this->setOptions(CURLOPT_FAILONERROR,true); } /** * Curl請求數據 * @param mixed $url url地址 * @param mixed $postdata get請求直接留空 post使用數組 array(name=>will,age=>24) * @return mixed url的內容 */ public function request($url,$postdata=false){ $this->setUrl($url); if(!$this->url){ return false; }else{ $this->setOptions(CURLOPT_URL,$this->url); } $this->setDefault(); if($postdata){ if(is_array($postdata)){ $this->setOptions(CURLOPT_POST,1); $this->setOption(CURLOPT_POSTFIELDS, $postdata); }else{ throw new Exception(post的數據必須是一個數組形式); } } $return=curl_exec($this->ch); if(!$return){ return false; }else{ return $return; } }}

作者:楓羽靈~

出處:520fyl.cnblogs.com/


推薦閱讀:

17種設計字體的創意方法
字體設計方法介紹及案例分析
閃瞎眼了!用PS設計打造一款2018黃金質感字體!
The Power of Art|對於線條也不能草率
《讓文字變得更精緻》

TAG:客戶端 | 字體設計 |