七夕啦,作為開發,妹子沒得撩就“撩”下服務器吧,妹子有得撩的同學那就左擁妹子右抱服務器吧,況且妹子是要禮物的,服務器又不用。好啦,長話短說再長說,祭出今天的工具——CURL(Client URL Library),當然今天以PHP的方式來使用這件工具。0. curl是個什么東西復制代碼 代碼如下:PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl html' target='_blank'>currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.這是PHP對于curl的一個解釋,簡單地說就是,curl是一個庫,能讓你通過URL和許多不同種的服務器進行勾搭、搭訕和深入交流,并且還支持許多協議。并且人家還說了curl可以支持https認證、http post、ftp上傳、代理、cookies、簡單口令認證等等功能啦。說了那么多其實沒什么感覺吧,在應用中才有感覺,我起初也是需要在服務器端向另一個服務器發起一個POST請求才開始接觸curl的,然后才有了感覺。在正式講怎么用之前啊,先提一句,你得先在你的PHP環境中安裝和啟用curl模塊,具體方式我就不講了,不同系統不同安裝方式,可以google查一下,或者查閱PHP官方的文檔,還挺簡單的。1. 拿來先試試手工具到手,先要把玩,試試順不順手,不然一拿來就用,把你自己的代碼搞得烏煙瘴氣還怎么去撩服務器呢?比如我們以著名的“測試網絡是否連接”的網站——百度為例,來嘗試下curl php // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, "baidu.com"); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); //echo output echo $output; // close curl resource to free up system resources curl_close($ch); 當你在本地環境瀏覽器打開這個php文件時,頁面出現的是百度的首頁,特么我剛才輸入的“localhost”呢?上面的代碼和注釋已經充分說明了這段代碼在干啥。$ch = curl_init(),創建了一個curl會話資源,成功返回一個句柄; curl_setopt($ch, CURLOPT_URL, "baidu.com"),設置URL,不用說;上面兩句可以合起來變一句$ch = curl_init("baidu.com");curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0)這是設置是否將響應結果存入變量,1是存入,0是直接echo出;$output = curl_exec($ch)執行,然后將響應結果存入$output變量,供下面echo;curl_close($ch)關閉這個curl會話資源。PHP中使用curl大致就是這么一個形式,其中第二步,通過curl_setopt方法來設置參數是最復雜也是最重要的,感興趣可以去看官方的關于可設置參數的詳細參考,長地讓你看得想吐,還是根據需要熟能生巧吧。小結一下,php中curl用法就是:創建curl會話 - 配置參數 - 執行 - 關閉會話。下面我們來看一些常用的情景,我們需要如何“打扮自己”(配置參數)才能正確“撩妹”(正確撩到服務器)。2. 打個招呼——GET和POST請求以及HTTPS協議處理先和服務器打個招呼吧,給服務器發個Hello看她怎么回,這里最方便的方式就是向服務器發出GET請求,當然POST這種小紙條也OK咯。2.1 GET請求我們以“在某著名同性交友網站github中搜索關鍵詞”為例//通過curl進行GET請求的案例 php // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, "https://github.com/search q=react"); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); //echo output echo $output; // close curl resource to free up system resources curl_close($ch); 1.默認請求方式是GET,所以不需要顯式指定GET方式; 2.https請求,非http請求,可能有人在各個地方看到過HTTPS請求需要加幾行代碼繞過SSL證書的檢查等方式來成功請求到資源,但是這里好像并不需要,原因是什么?復制代碼 代碼如下:The two Curl options are defined as:CURLOPT_SSL_VERIFYPEER - verify the peer's SSL certificate CURLOPT_SSL_VERIFYHOST - verify the certificate's name against host They both default to true in Curl, and shouldn't be disabled unless you've got a good reason. Disabling them is generally only needed if you're sending requests to servers with invalid or self-signed certificates, which is only usually an issue in development. Any publicly-facing site should be presenting a valid certificate, and by disabling these options you're potentially opening yourself up to security issues.即,除非用了非法或者自制的證書,這大多數出現在開發環境中,你才將這兩行設置為false以避開ssl證書檢查,否者不需要這么做,這么做是不安全的做法。2.2 POST請求那如何進行POST請求呢?為了測試,先在某個測試服務器傳了一個接收POST的腳本://testRespond.php php $phpInput=file_get_contents('php://input'); echo urldecode($phpInput);