php 【脚本】调接口同步写入数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 【脚本】调接口同步写入数据库相关的知识,希望对你有一定的参考价值。

<?php
require_once(dirname(__FILE__) . '/../webapp/config.php');
// +---------------------------------------------------------------------------+
// | An absolute filesystem path to the agavi/agavi.php script.                |
// +---------------------------------------------------------------------------+
require_once(AG_APP_DIR . '/agavi.php');

Context::getInstance()->initialize();

if ($argc != 2) {
    echo "please input command";
    exit;
}
$action = $argv[1];
switch ($action) {
    case "first":
        doFirstImport();
    case "latlng":
        doLatLngUpdate();

}

function doFirstImport()
{
    //https://365shequ.com/app/contents/sjw_data?timestamp={time()}&signature={md5('wjShequSjwDataTransmission,{time()}')}
    $apiUrlFormat = "%s?timestamp=%d&signature=%s";
    $apiUrl = 'https://365shequ.com/app/contents/sjw_data';
    $time = time();
    $signature = md5('wjShequSjwDataTransmission' . ',' . $time);
    $finalApiUrl = sprintf($apiUrlFormat, $apiUrl, $time, $signature);
    $result = curlGet($finalApiUrl);
    if ($result != '请求出错') {
        $data = json_decode($result, true);
        $willInsertData = $data['data']['data'];
        if ($willInsertData) {
            foreach ($willInsertData as $item) {
                insertCommunityData($item);
            }
        }


    } else {
        echo "error";
    }
}

//更新经纬度信息
function doLatLngUpdate(){
    //https://365shequ.com/app/contents/sjw_data?timestamp={time()}&signature={md5('wjShequSjwDataTransmission,{time()}')}
    $apiUrlFormat = "%s?timestamp=%d&signature=%s";
    $apiUrl = 'https://365shequ.com/app/contents/sjw_data';
    $time = time();
    $signature = md5('wjShequSjwDataTransmission' . ',' . $time);
    $finalApiUrl = sprintf($apiUrlFormat, $apiUrl, $time, $signature);
    $result = curlGet($finalApiUrl);
    if ($result != '请求出错') {
        $data = json_decode($result, true);
        $willInsertData = $data['data']['data'];
        if ($willInsertData) {
            foreach ($willInsertData as $item) {
                updateCommunityLatLng($item);
            }
        }
    } else {
        echo "error";
    }

}

//更新经纬度信息
function updateCommunityLatLng($data)
{
    $conn = Propel::getConnection();

    //检查数据是否存在
    $orgId = $data['id'];
    // 从第四个开始更新,因为前三个都是其他作用的数据。
    if($orgId > 3){
        if (!isOrgExist($orgId)) {
            if ($data['latitude'] && $data['longitude']) {
                $map_lat_lng = $data['latitude'] . ',' . $data['longitude'];
                $sql = "UPDATE t_casual_org SET map_lat_lng = '{$map_lat_lng}',city = 1043 where org_id=" . $orgId;
                $conn->executeUpdate($sql);
            }
        }
    }
}



function curlGet($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 2);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
}


function insertCommunityData($data)
{
    $conn = Propel::getConnection();

    //检查数据是否存在
    $orgId = $data['id'];
    if (isOrgExist($orgId)) {
        $insertData = [];
        $insertData['org_id'] = $data['id'];
        $insertData['org_name'] = $data['shortName'];
        $insertData['org_phone'] = $data['phone'];
        $insertData['city'] = 1043;
        if ($data['latitude'] && $data['lontitude']) {
            $insertData['map_lat_lon'] = $data['latitude'] . ',' . $data['longitude'];
        }
        $insertData['save_time'] = time();
        $insertKeys = array_keys($insertData);
        $sql = "INSERT INTO t_casual_org (" . implode(",", $insertKeys) . ") VALUES (" . substr(str_repeat('?,', count($insertKeys)), 0, -1) . ")";
        $stmt = $conn->prepareStatement($sql);
        $insertValues = array_values($insertData);
        $r = $stmt->executeUpdate($insertValues);
        if (!$r) {
            echo $data['id'] . 'sql error';
        }
    }

}

function isOrgExist($orgId)
{
    //
    $conn = Propel::getConnection();
    $sql = "select COUNT(*) from t_casual_org where org_id=?";
    $stmt = $conn->prepareStatement($sql);
    $rs = $stmt->executeQuery([$orgId], ResultSet::FETCHMODE_NUM);
    $count = 0;
    if ($rs->next()) {
        $count = $rs->getRow()[0];
    }
    if ($count) {
//        echo $orgId . ",exist";
        return false;
    }
    return true;

}

以上是关于php 【脚本】调接口同步写入数据库的主要内容,如果未能解决你的问题,请参考以下文章

PHP性能调优---php-fpm中启用慢日志配置(用于检测执行较慢的PHP脚本)

php 同时修改两个数据库

PHP 跨站点通用脚本(写入Cookie数据)

php脚本写入SQL数据库[关闭]

php 在自己的线程中运行php脚本而不阻止调用者

php写入文件来调试接口数据