当前位置:首页>>问题

测算系统某个测算项目对接百度广告回传api流程

1、在ctl_ffsm_h5_index.php文件中,找到对应测算项目函数,比如shengxiao,在函数中添加代码:$bd_vid = req::item('bd_vid');//将百度广告带来的bd_vid存起来 用于回传给百度if($bd_vid) { $bd_vid_url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI&

admin

1、在ctl_ffsm_h5_index.php文件中,找到对应测算项目函数,比如shengxiao,在函数中添加代码:

$bd_vid = req::item('bd_vid');

//将百度广告带来的bd_vid存起来 用于回传给百度

if($bd_vid) {

      $bd_vid_url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

       setcookie("bd_vid_url", $bd_vid_url, 0, '/');

       setcookie("bd_vid", $bd_vid, 0, '/');

}

上面的代码意思就是接收百度广告页传来的bd_vid,并把广告页地址保存到cookie里面,方便后面回传给百度用

2、将APIDemo.php回传文件上传到ffsm目录里,代码

<?php
session_start();

//echo ''.$_COOKIE['bd_vid_url'];die;
$bd_url = $_COOKIE['bd_vid_url'];

/**
 * Class APIDemo API回传数据Demo
 */
class APIDemo {

    const BAIDU_OCPC_URL = 'https://ocpc.baidu.com/ocpcapi/api/uploadConvertData';
    const RETRY_TIMES = 3;

    /**
     * @param $token
     * @param $conversionTypes
     * @return bool 发送成功返回true,失败返回false
     */
    public function sendConvertData($token, $conversionTypes) {
        $reqData = array('token' => $token, 'conversionTypes' => $conversionTypes);
        $reqData = json_encode($reqData);
        // 发送完整的请求数据
        // do some log
        print_r('req data: ' . $reqData . "\n");
        // 向百度发送数据
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, self::BAIDU_OCPC_URL);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $reqData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($reqData)
            )
        );
        // 添加重试,重试次数为3
        for ($i = 0; $i < self::RETRY_TIMES; $i++) {
            $response = curl_exec($ch);
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if ($httpCode === 200) {
                // 打印返回结果
                // do some log
                print_r('retry times: ' . $i . ' res: ' . $response . "\n");
                $res = json_decode($response, true);
                // status为4,代表服务端异常,可添加重试
                $status = $res['header']['status'];
                if ($status !== 4) {
                    curl_close($ch);
                    return $status === 0;
                }
            }
        }
        curl_close($ch);
        return false;
    }
}


$token = '你的百度广告的token';
// 编辑一条转化数据
$cv = array(
    //'logidUrl' => 'http://xxx.xxx.com/?ac=shengxiao&bd_vid='.$_COOKIE['bd_vid'], // 您的落地页url
    'logidUrl' => $_COOKIE['bd_vid_url'], // 您的落地页url
    'newType' => 10 // 转化类型请按实际情况填写
);
// 此处仅为demo, conversionTypes支持添加更多数据
$conversionTypes = array($cv);
$demo = new APIDemo();
$demo->sendConvertData($token, $conversionTypes);
?>

3、付款成功后,跳转到测算结果页面,在测算结果页面,利用js的ajax来执行APIDemo.php回传文件

<script>

$(function(){

    $.ajax({
		url:"http://xxx.xxx.cn/APIDemo.php",
		//dataType:"json",   //返回格式为json
		//async:true,//请求是否异步,默认为异步,这也是ajax重要特性
		//data:{"bd_vid":"<{$bd_vid}>"},    //参数值 
		type:"GET",   //请求方式
		success:function(e){
			console.log("success");
		},
		error:function(e){
			console.log("hits,sorry");
		}
	});


    
});
</script>

4、如果付款成功后,没有直接跳转到测算结果页面,那就在跳转的页面加上下面代码:

<script type="text/javascript" src="../static/js/jquery.min.js"></script>
<script>

$(function(){

    $.ajax({
		url:"http://xxx.xxx.cn/APIDemo.php",
		//dataType:"json",   //返回格式为json
		//async:true,//请求是否异步,默认为异步,这也是ajax重要特性
		//data:{"bd_vid":"<{$bd_vid}>"},    //参数值 
		type:"GET",   //请求方式
		success:function(e){
			console.log("success");
		},
		error:function(e){
			console.log("hits,sorry");
		}
	});


    
});
</script>

5、好了,对接流程就是这样。


返回顶部