多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] >[warning] 使用前请务必 load()->func('communication'); #### GET请求某个URL(简单) >[info] ihttp_get($url) * $url 要获取内容的URL,必须是以http或是https开头 *返回值*` error [错误结构](http://www.kancloud.cn/donknap/we7/134630) success ``` array ( 'code' => 200 //http 状态码 'status' => OK //http 状态信息 'responseline' => HTTP/1.1 200 OK 'headers' => array ( //返回头部的一些信息,这里是直接显示百度的返回头部信息 //具体功能函数不在这里赘述,可以查看HTTP相关文档 'Server' => bfe/1.0.8.18 'Date' => Wed, 21 Sep 2016 03:23:54 GMT 'Content-Type' => text/html; charset=utf-8 'Connection' => close 'Vary' => Accept-Encoding 'P3P' => CP=" OTI DSP COR IVA OUR IND COM " 'Cache-Control' => private 'Cxy_all' => baidu+7cf6bc2c9c5ae104a1dad56cb1e2a027 'Expires' => Wed, 21 Sep 2016 03:22:58 GMT 'X-Powered-By' => HPHP 'X-UA-Compatible' => IE=Edge,chrome=1 'Strict-Transport-Security' => max-age=604800 'BDPAGETYPE' => 1 'BDQID' => 0x9f936d6300036048 'BDUSERID' => 0 // 此处返回服务器给客户端设置的Cookie信息 // 下面的例子中会有怎么使用的例子 'Set-Cookie' => array ( '0' => H_PS_PSSID=1437_21014_17944_21127_; path=/; domain=.baidu.com '1' => __bsi=1222377018390; expires=Wed, 21-Sep-16 03:23:59 GMT; domain=www.baidu.com; path=/ ) ) 'content' => '<!DOCTYPE html><!--STATUS OK--><html><head>..省略3千字...</body></html>' //网页的HTML内容 ) ``` *示例* ``` load()->func('communication'); $response = ihttp_get('http://baidu.com'); print_r($response['content']); ``` #### POST请求某个URL(简单) >[info] ihttp_post($url, $data) * $url 要获取内容的URL,必须是以http或是https开头 * $data 要提交的表单数据,数组格式,如果是上传文件,则以 @ + 文件物理路径 书写 *返回值*` 与 ihttp_get 函数相同 *示例* ``` //微擎会自动处理兼容为new CURLFile的写法 $fullname = ATTACHMENT_ROOT . '/images/691/2016/08/tWSNp69sp82ErcW8c0NYSm86s86Cl0.jpg'; $sendapi = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$token}"; $data = array( 'name' => 'mizhou', 'media' => '@'.$fullname ); load()->func('communication'); $response = ihttp_post($sendapi, $data); ``` #### 请求某个URL(高级) >[info] ihttp_request($url, $post = '', $extra = array(), $timeout = 60) * $url 要获取内容的URL,必须是以http或是https开头 * $post 数组格式,要POST请求的数据,为空时则会以GET形式请求 * $extra 请求附加值,下面会例子中会演示使用方法 * $timeout 超时时间 *返回值*` 与 ihttp_get 函数相同 *示例* ###### 附加表单数据请求到某个URL ``` load()->func('communication'); $loginurl = 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN'; $post = array( 'username' => $username, 'pwd' => $password, ); $response = ihttp_request($loginurl, $post); if (is_error($response)) { return false; } return true; ``` ###### 请求时附加一些头部信息 ``` //此实例为获取微信图片 //微信的图片需要要求必须有引用页,程序中无法直接调用,以下代码实现一个具体引用页的请求来获取微信图片 load()->func('communication'); //微信图片 $image = trim($_GPC['attach']); if(empty($image)) { exit(); } $content = ihttp_request($image, '', array('CURLOPT_REFERER' => 'http://www.qq.com')); header('Content-Type:image/jpg'); echo $content['content']; exit(); ``` ``` //此实例为获取支付宝的支付地址 //支付宝的接口通过301跳转来发送给客户端跳转地址,程序中为了获取此url,故设置请求时不自动跳转 load()->func('communication'); $response = ihttp_request(ALIPAY_GATEWAY . '?' . http_build_query($set, '', '&'), array(), array('CURLOPT_FOLLOWLOCATION' => 0)); return array('url' => $response['headers']['Location']); ``` ``` //此实例为模拟微信请求地址,请求数据为xml格式 load()->func('communication'); $response = ihttp_request($item['apiurl'], $message, array('CURLOPT_HTTPHEADER' => array('Content-Type: text/xml; charset=utf-8'))); return $response['content']; ``` ``` //此实例为附件cookie请求 //有些地址可能以cookie来判断是否是恶意程序请求 load()->func('communication'); $response = ihttp_get($url); $cookiejar = $response['headers']['Set-Cookie']; $response = ihttp_request($urlpost, array('num' => 1), array( 'CURLOPT_COOKIE' => implode('; ', $cookiejar), 'CURLOPT_REFERER' => 'https://selfsolve.apple.com/agreementWarrantyDynamic.do', )); ```