以一个拉取基本档案中的品牌为例:
主函数:
class brand{
function __construct() {
$this->mdl_task = new MdlTask();
}
function get_list(array & $request,array & $response,array & $app){
$stime=microtime(true);//1.时间戳
$mdl_brand = new MdlBrand();
$response['message'] = "";
$task = $this->mdl_task->get_task_by_code("SYS_TASK_ERP2_BRAND_GET_LIST");
$update = 0;
$insert = 0;
$page = 1;
$add_time = add_time();
$next = true;
do{
//接口地址拼装
$url = get_erp2_api_url("EfastBusiness_DoBusinessProcess&OP=328&Page=".$page."&Count=1000&Type=1&NatureType=2");
$ret = post_submit($url);//2.远程提交
$ret = object_to_array(json_decode($ret));//对象转数组
//var_dump($ret);exit;
if(isset($ret['data']) && $ret['msg'] == 'success') {
foreach ($ret['data']['list'] as $data) {
$brand = $mdl_brand->get_brand_by_code($data['PPMC']);
$brand_data = array(
"brand_code" => $data['PPMC'],
"brand_name" => $data['PPDM'],
"trd_type" => "erp2",
"trd_time" => $add_time,
);
if ($brand) {
$mdl_brand->update_by_id($brand['brand_id'], $brand_data);
$update++;
} else {
$mdl_brand->add_action_mode($brand_data);
$insert++;
}
}
if ($page >= $ret['data']['info']['pages']) {
$next = false;
}
$page++;
}else{
return $response=return_value('-1','请求失败,请检查配置');
}
}while ($next);
$this->mdl_task->update_by_id($task['task_id'],array("update_time"=>$add_time));
$response['message'] .= "拉取完成,新增".$insert."个,更新".$update."个";
$response['status'] = 1;
$etime = microtime(true);
$total = $etime - $stime;
app_log($total,$response);
}
}
解释:
1.microtime();返回unix时间戳。
<?php
echo microtime();
echo "<br>";
echo microtime(true);
?>
结果如下:
0.00000600 1283757623
1283757623
2.POST远程提交
/**
* POST远程提交
* $url 远程链接
* $post_data 参数
*/
function post_submit($url, $post_data = "", $param=array()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //跳过SSL证书检查
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($ch, CURLOPT_URL, $url);
if (isset($param['header'])){
curl_setopt($ch,CURLOPT_HTTPHEADER,$param['header']);
}else{
curl_setopt($ch, CURLOPT_HEADER, 0);
}
if (isset($param['followlocation'])){
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $param['followlocation']); //用来跟随重定向页面
}
if (isset($param['referer_url']))
curl_setopt($ch, CURLOPT_REFERER, $param['referer_url']);
if (isset($param['save_cookie']))
curl_setopt($ch, CURLOPT_COOKIEJAR, $param['save_cookie']);
if (isset($param['use_cookie']))
curl_setopt($ch, CURLOPT_COOKIEFILE, $param['use_cookie']);
if ($post_data != "") {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
$info = curl_exec($ch); //执行一个cURL会话,并把它传递给浏览器
curl_close($ch);
return $info;
}
3.对象转化为数组
function object_to_array($obj) {
$arr = array();
if (is_object($obj) || is_array($obj)) {
foreach ($obj as $key => $val) {
if (!is_object($val)) {
if (is_array($val)) {
$arr[$key] = object_to_array($val);
} else {
$arr[$key] = $val;
}
} else {
$arr[$key] = object_to_array($val);
}
}
}
return $arr;
}
转载于:https://my.oschina.net/wsyblog/blog/600531