当我们在做php开发的时候,非常多时候须要对接口进行測试,或者更方便的调用一些已有模块的接口,取到结果并进行兴许操作,我们能够通过curl进行模拟提交post和get请求,来去实现这些功能。
以下是对curl的post和get的封装
<?php
/**
* Created by PhpStorm.
* User: thinkpad
* Date: 2015/7/17 0017
* Time: 13:24
*/
class Action
{
public static function curl_get($url){
$testurl = $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $testurl);
//參数为1表示数据传输。为0表示直接输出显示。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//參数为0表示不带头文件,为1表示带头文件
curl_setopt($ch, CURLOPT_HEADER,0);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
/*
* url:訪问路径
* array:要传递的数组
* */
public static function curl_post($url,$array){
$curl = curl_init();
//设置提交的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 0);
//设置获取的信息以文件流的形式返回。而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
//设置post数据
$post_data = $array;
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//运行命令
$data = curl_exec($curl);
//关闭URL请求
curl_close($curl);
//获得数据并返回
return $data;
}
}
之后就能够通过CURL::curl_post($url,$array)或者CURL::curl_get($url);的方式调用接口并得到数据了。
