ホームページ  >  記事  >  バックエンド開発  >  PHPのcurlカプセル化の使用例_PHPチュートリアル

PHPのcurlカプセル化の使用例_PHPチュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:14:51954ブラウズ

PHPのcurlカプセル化クラスの使用例

関数を使用する前に、PHPのcurlモジュール(libeay32.dll、ssleay32.dll、php5ts.dll、php_curl.dll)を開く必要があります

PHP CURL関数ライブラリを有効にする手順

1) Windows/php.ini ファイル内の ;extension=php_curl.dll を削除します。 /*echo phpinfo() を使用して php.ini*/
のパスを確認します。 2) php5/libeay32.dll と ssleay32.dll をシステム ディレクトリ windows/
にコピーします。 3).Apacheを再起動します

コードは次のとおりです:

コードは次のとおりです:

include_once('curl.class.php');
$aa =新しいカール('');
$curlOptions = array(
CURLOPT_URL => "http://www.xx.com/addTicket.jsp", //URLにアクセスします
CURLOPT_RETURTRANSFER => true, //結果を取得し、文字列として返します
CURLOPT_REFERER => "ww.ww.ww/zw2",
CURLOPT_HTTPHEADER => array('X-FORWARDED-FOR:139.197.14.19', 'CLIENT-IP:127.0.0.1','プロキシ-クライアント-IP:139.197.14.19','WL-プロキシ-クライアント-IP:139.197 .14.19' )、
CURLOPT_HEADER => 1, //リターンヘッダー情報を取得します
//CURLOPT_SSL_VERIFYPEER => false, //SSL暗号化をサポート
CURLOPT_POST => true, //POSTパラメータを付けて送信
CURLOPT_POSTFIELDS => 'ids=897&Submit=%E6%8A%95%E7%A5%A8', //リクエストされたPOSTパラメータ文字列
CURLOPT_TIMEOUT => $aa->timeout //応答を待つ時間
);
echo $aa->getResponseText($curlOptions);


カル処理カテゴリ:

コードは次のとおりです:

クラスカール
{
パブリック $cookieFile;
パブリック $timeout = 160;
パブリック関数 __construct($dir){
$this->cookieFile = $this->getTemporaryCookieFileName($dir);
}
/**
* CURLパラメータを設定し、リクエストを送信して応答コンテンツを取得します
* @アクセス非公開
* @param $curlOptions 配列 Curl 設定パラメータ配列
* @return string|false アクセスが成功した場合は取得した情報を文字列形式で返します、それ以外の場合は false を返します
*/
パブリック関数 getResponseText($curlOptions) {
/* CURLOPT_RETURNTRANSFER を true に設定します */
if(!isset($curlOptions[CURLOPT_RETURNTRANSFER]) || $curlOptions[CURLOPT_RETURNTRANSFER] == false) {
$curlOptions[CURLOPT_RETURNTRANSFER] = true;
}
/*カールモジュールを初期化します*/
$curl =curl_init();
/* カール オプションを設定します */
curl_setopt_array($curl, $curlOptions);
/* リクエストを送信し、応答情報を取得します */
$responseText = '';
試してみてください{
$responseText =curl_exec($curl);
if(($errno =curl_errno($curl)) != CURLM_OK) {
$errmsg =curl_error($curl);
新しい例外をスローします($errmsg, $errno);
}
} catch (例外 $e) {
//例外DisposeFunction($e);
//print_r($e);
$responseText = false;
}
/*カールモジュールを閉じる*/
curl_close($curl);
/* 結果を返します */
$responseText を返します;
}
/**
* Unicode文字列(u0000)をutf-8文字列に変換するツール関数
* @アクセス非公開
* @静的
* @param $string string Unicode 文字列
* @return 文字列 utf-8 文字列
*/
パブリック関数 unicodeToUtf8($string) {
$string = str_replace('u', '', strto lower($string));
$length = strlen($string) / 4;
$stringResult = '';
for($i = 0; $i < $length; $i++) {
$charUnicodeHex = substr($string, $i * 4, 4);
$unicodeCode = hexdec($charUnicodeHex);
$utf8Code = '';
if($unicodeCode < 128) {
$utf8Code = chr($unicodeCode);
} else if($unicodeCode < 2048) {
$utf8Code .= chr(192 + (($unicodeCode - ($unicodeCode % 64)) / 64));
$utf8Code .= chr(128 + ($unicodeCode % 64));
} その他 {
$utf8Code .= chr(224 + (($unicodeCode - ($unicodeCode % 4096)) / 4096));
$utf8Code .= chr(128 + ((($unicodeCode % 4096) - ($unicodeCode % 64)) / 64));
$utf8Code .= chr(128 + ($unicodeCode % 64));
}
$stringResult .= $utf8Code;
}
$stringResult を返します;
}
プライベート関数 getTemporaryCookieFileName($dir='.') {
return (str_replace("", '/', tempnam($dir, 'tmp')));
}
}



例 2

コードは次のとおりです:

//カール类
クラスカール
{
    関数 Curl(){
        true を返します;
    }
    
    functionexecute($method, $url, $fields='', $userAgent='', $httpHeaders='', $username='', $password=''){
        $ch = Curl::create();
        if(false === $ch){
            false を返します;
        }
        if(is_string($url) && strlen($url)){
            $ret =curl_setopt($ch, CURLOPT_URL, $url);
        }その他{
            false を返します;
        }
        // 否か表示头部情報
        curl_setopt($ch, CURLOPT_HEADER, false);
        //
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if($ユーザー名 != ''){
            curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
        }
        $method = strto lower($method);
        if('post' == $method){
            curl_setopt($ch, CURLOPT_POST, true);
            if(is_array($fields)){
                $sets = array();
                foreach ($fields AS $key => $val){
                    $sets[] = $key 。 '=' 。 urlencode($val);
                }
                $fields = implode('&',$sets);
            }
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        }else if('put' == $method){
            curl_setopt($ch, CURLOPT_PUT, true);
        }
        //curl_setopt($ch, CURLOPT_PROGRESS, true);
        //curl_setopt($ch, CURLOPT_VERBOSE, true);
        //curl_setopt($ch, CURLOPT_MUTE, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);//curl超時間秒数を設定します
        if(strlen($userAgent)){
            curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        }
        if(is_array($httpHeaders)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
        }
        $ret =curl_exec($ch);
        if(curl_errno($ch)){
            curl_close($ch);
            return array(curl_error($ch),curl_errno($ch));
        }その他{
            curl_close($ch);
            if(!is_string($ret) || !strlen($ret)){
                false を返します;
            }
            $ret を返します;
        }
    }
    
    function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
        $ret = Curl::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);
        if(false === $ret){
            false を返します;
        }
        if(is_array($ret)){
            false を返します;
        }
        $ret を返します;
    }
    
    関数 get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
        $ret = Curl::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);
        if(false === $ret){
            false を返します;
        }
        if(is_array($ret)){
            false を返します;
        }
        $ret を返します;
    }
    
    関数 create(){
        $ch = null;
        if(!function_exists('curl_init')){
            false を返します;
        }
        $ch =curl_init();
        if(!is_resource($ch)){
            false を返します;
        }
        $ch を返します;
    }
}
?>


用法

入手方法:

代码如下:

$curl = 新しい Curl();
$curl->get('http://www.jb51.net/');


投稿用法:

复制代码代码如下:

$curl = 新しい Curl();
$curl->get('http://www.jb51.net/', 'p=1&time=0');

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/907710.html技術記事 phpのcurl封装类用法实例使用関数数之前我们要要把phpカール模块打开(libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll) 开启phpcurl函数库的步骤 1).去...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。