分类 PHP 下的文章

YAF说明文档

Yaf - Yet Another Framework

Build Status
PHP framework written in c and built as a PHP extension.

Requirement

  • PHP 5.2 +

Install

Install Yaf

Yaf is an PECL extension, thus you can simply install it by:

$pecl install yaf

Compile Yaf in Linux

$/path/to/phpize
$./configure --with-php-config=/path/to/php-config
$make && make install

For windows

Yaf binary dlls could be found at http://code.google.com/p/yafphp/downloads/list

Document

Yaf manual could be found at: http://www.php.net/manual/en/book.yaf.php

IRC

efnet.org #php.yaf

For IDE

you could find a documented prototype script here: https://github.com/elad-yosifon/php-yaf-doc

Tutorial

layout

PHP 之 CURL学习(一)

  • 此文件为curl学习例子,参考http://www.chinaz.com/program/2010/0119/104346.shtmlPHP手册
  • libcurl 目前支持http、https、ftp、gopher、telnet、dict、file和ldap协议。
  • libcurl 同时也支持HTTPS认证、HTTP POST、HTTP PUT、FTP上传、HTTP基于表单的上传、代理、COOKIES和用户名+密码的验证

Example1

  • cURL的请求的基本步骤:
    1.初始化
    2.设置变量
    3.执行并获取结果
    4.释放cURL句柄
$ch = curl_init(); //1、初始化
curl_setopt($ch, CURLOPT_URL, 'http://www.baidu.com'); //2、设置选项、包括URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //CURLOPT_RETURNTRANSFER 将curl_exec()获取的信息以流文件形式返回,而不是直接输出
curl_setopt($ch, CURLOPT_HEADER, 1); //CURLOPT_HEADER 启用时会将头文件的信息作为数据流输出
$output = curl_exec($ch); //3、执行 curl_exec失败时返回flase,判断时应该用===
curl_close($ch); //4、释放句柄

Example2

  • 写两个简单的函数用curl来发送POST和GET请求
    curl_setopt_array() 为cURL传输回话批量设置选项
/**
*Send a POST request using cURL
*@param string $url to request
*@param array $post values to send
*@param array $options for url
*/
function curl_post($url, array $post = NULL, array $options = array()) {
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
CURLOPT_POSTFIELDS => http_build_query($post)
);

$ch = curl_init();
curl_setopt_array($ch, $options + $defaults);//数组相加 根据key将在后一个数组而不再前一个数组中的item加入第一个数组中(任意一个数组不是数组导致Faltal error)
if(false === $result = curl_exec($ch)) {
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}

/**
*Send a GET request using cURL
*@param string $url to request
*@param array $get values to send
*@param array $options for cURL
*/
function curl_get($url, array $get = NULL, array $options = array()) {
$defaults = array(
CURLOPT_URL => $url.($get ? (strpos($url, '?') === false ? '?' : '&').http_build_query($get) : ''),
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 4
);

$ch = curl_init();
curl_setopt_array($ch, $options + $defaults);
if(false === $result = curl_exec($ch)) {
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}

Example3

  • 通过cURL上传文件
$url = 'http://localhost/log.php';
$post_data = array(
'name' => 'myname',
'file' => '@d:\test.jpg' //上传的本地文件要加@符号
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); //可有可无
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
if(false === $result = curl_exec($ch)) {
trigger_error(curl_error($ch));
}
curl_close($ch);