123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace app\common\components;
- use Yii;
- class BaiduCpc {
- const BAIDU_OCPC_URL = 'https://ocpc.baidu.com/ocpcapi/api/uploadConvertData';
- const RETRY_TIMES = 3;
-
- public function sendConvertData($token, $conversionTypes) {
- $reqData = array('token' => $token, 'conversionTypes' => $conversionTypes);
- $reqData = json_encode($reqData);
-
-
-
-
- $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)
- )
- );
-
- for ($i = 0; $i < self::RETRY_TIMES; $i++) {
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- if ($httpCode === 200) {
-
-
-
- $res = json_decode($response, true);
-
- $status = $res['header']['status'];
- if ($status !== 4) {
- curl_close($ch);
- return $status === 0;
- }
- }
- }
- curl_close($ch);
- return false;
- }
- }
|