国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
PHP之curl

PHP之curl

Jun 13, 2016 am 09:22 AM

PHP之curl

?當(dāng)我第一次接觸curl的時(shí)候,看文檔,以及網(wǎng)上search各種資料,官方(http://cn2.php.net/manual/en/intro.curl.php)的解釋是,這是某大牛寫(xiě)的一個(gè)libcurl庫(kù),PHP支持該擴(kuò)展庫(kù),允許我們以各種不同的協(xié)議(http、https、ftp、telnet、file...)訪問(wèn)各種不同的服務(wù)器,支持post、put、ftp或基于表單的文件上傳,支持cookie、代理等等。

?

? ? ?好像還是似懂非懂,我覺(jué)得最簡(jiǎn)單的解釋是,curl是一個(gè)工具集,由libcurl擴(kuò)展庫(kù)支持,包含一些函數(shù),使用這些函數(shù)可以模擬我們來(lái)訪問(wèn)某些地址,也就是,我們手動(dòng)在瀏覽器地址欄里面輸入http://www.baidu.com,來(lái)訪問(wèn)百度,curl就可以代替這種手動(dòng)操作,以程序的形式來(lái)實(shí)現(xiàn)這一過(guò)程,這個(gè)操作、過(guò)程得到的結(jié)果,是瀏覽器給了我們一個(gè)百度搜索的首頁(yè)。

?

? ? ?既然curl是一個(gè)工具集,有很多函數(shù)可供調(diào)用,可以想象成,在地址欄輸入U(xiǎn)RL(可能會(huì)帶一些參數(shù)),當(dāng)輸入完點(diǎn)擊回車(chē)的時(shí)候,相當(dāng)于調(diào)用了某些函數(shù),這些函數(shù)在百度某臺(tái)或某幾臺(tái)服務(wù)器上的腳本上寫(xiě)著,函數(shù)運(yùn)行完后會(huì)有某種效果,比如返回了值,或者沒(méi)返回值,或者打印了一些東西,在這里呈現(xiàn)了一個(gè)頁(yè)面給你,而curl能實(shí)現(xiàn)的功能,比簡(jiǎn)單展示一個(gè)頁(yè)面多得多,這些各自不同的功能是通過(guò)curl_setopt函數(shù)來(lái)實(shí)現(xiàn)的。比如在接SDK的時(shí)候,別人提供的是訪問(wèn)地址形式的API,這時(shí)curl就大顯身手。

?

? ? ?1.get實(shí)現(xiàn)

?

復(fù)制代碼

? ? $url = 'http://www.somesite.com';

? ? $data = array('username'=>'Peter', 'password'=>12345);

? ? function get($url, $data = array()){

? ? ? ? $ch = curl_init(); ? // 初始化一個(gè)curl資源類(lèi)型變量

? ? ? ??

? ? ? ? /*設(shè)置訪問(wèn)的選項(xiàng)*/

? ? ? ? curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); ?// 啟用時(shí)會(huì)將服務(wù)器服務(wù)器返回的Location: 放在header中遞歸的返回給服務(wù)器

? ? ? ? curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); ?// 將獲得的數(shù)據(jù)返回而不是直接在頁(yè)面上輸出

? ? ? ? curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP ); ?// 設(shè)置訪問(wèn)地址用的協(xié)議類(lèi)型為HTTP ? ? ? ?

? ? ? ? curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); ?// 訪問(wèn)的超時(shí)時(shí)間限制為15s

? ? ? ? $url = url.'?'.http_build_query($data); ?

? ? ? ? curl_setopt($ch, CURLOPT_URL, $url); ?// 設(shè)置即將訪問(wèn)的URL

?

? ? ? ? $result = curl_exec($ch); ?// 執(zhí)行本次訪問(wèn),返回一個(gè)結(jié)果

? ? ? ? // ... ? ? ? ? ? ? ? ? ? ? // 針對(duì)結(jié)果的正確與否做一些操作

? ? ? ? return $result;

? ? }

? ? // $result = get(%url, $data); ?// 調(diào)用

復(fù)制代碼

? ? ? 2.post實(shí)現(xiàn)

?

復(fù)制代碼

? ? function post($url, $data = array()){

? ? ? ? $ch = curl_init();?

? ? ? ? ??

? ? ? ? curl_setopt($ch, CURLOPT_POST, true); ?// 設(shè)置為post傳遞形式

? ? ? ? curl_setopt($ch, CURLOPT_POSTFIELDS, $data); ?// 設(shè)置post傳遞的數(shù)據(jù)

? ? ? ? curl_setopt($ch, CURLOPT_URL, $url);

? ? ? ? curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); ?// 設(shè)置數(shù)據(jù)以文件流的形式返回

? ? ? ? curl_setopt($ch, CURLOPT_USERAGENT, ''); ?// 將用戶(hù)代理置空

? ? ? ? curl_setopt($ch, CURLOPT_HEADER, false); ?// 設(shè)置不顯示頭信息

? ? ? ? ??

? ? ? ? $result = curl_exec($ch);

? ? ? ? $info = curl_getinfo($ch); ? // 獲取本次訪問(wèn)資源的相關(guān)信息

? ? ? ? ??

? ? ? ? return $result;

? ? } ??

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)