* [GET请求某个URL(简单)](https://ihavenolimitations.xyz/donknap/we7/212602#GETURL_5)
* [POST请求某个URL(简单)](https://ihavenolimitations.xyz/donknap/we7/212602#POSTURL_59)
* [请求某个URL(高级)](https://ihavenolimitations.xyz/donknap/we7/212602#URL_83)
* [附加表单数据请求到某个URL](https://ihavenolimitations.xyz/donknap/we7/212602#URL_98)
* [请求时附加一些头部信息](https://ihavenolimitations.xyz/donknap/we7/212602#_113)
> 使用前请务必 load()->func('communication');
#### GET请求某个URL(简单)
> ihttp\_get($url)
* $url 要获取内容的URL,必须是以http或是https开头
*返回值*`
error
[错误结构](http://ihavenolimitations.xyz/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(简单)
> 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(高级)
> 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',
));
~~~
- php调试
- php记录
- 常用循环
- 日期时间转换
- 时间格式
- mktime获取今日昨日本月时间戳
- http_build_query生成请求字符串
- PHP取整的几种方法
- 微擎记录
- W全局变量
- Http请求
- 人人商城二开记录
- 数据处理
- 常用查询
- 会员信息
- 确认收货处理
- 维权处理
- 设置读取和配置
- 报单商品类型属性
- 团队佣金
- 自动升级
- 业绩分红
- 前端html和js
- html按钮提交处理
- 引用js传参
- 表单
- 表单判断
- 任务执行
- 提示跳转页
- 全局变量
- 订单交易
- DIY设置
- 系统菜单
- mysql记录
- 创建表-14
- html和js
- 模板中使用js变量传参
- js表单处理
- 表单判断
- js页面跳转刷新
- thinkphp帮助
- 导入导出excel