$mediaData['introduction'] = $data['introduction']; } $return = $wechat->uploadMedia($mediaData); if ($return['hasError']) { unlink($targetFile); $returnArr = array('code' => 400, 'err' => $return['err']); } else { // --------------- update server cache ------------------- $response = $return['response']; $json_string = $wechat->raw_json_encode($response); if ($type == $wechat::MSGTYPE_IMAGE) { $_query = "INSERT INTO media_image ( %s ) VALUES ( %s ) "; $bindArr = array('media_id' => $response['media_id'], 'name' => $filename, 'url' => $response['url'], 'local_path' => $targetPath . $targetFileName, 'update_time' => time(), 'raw_data' => $json_string); $bindKeys = implode(',', array_keys($bindArr)); // to sting media_id,name, .... $bindPlaceHolders = ':' . str_replace(',', ',:', $bindKeys); // to sting :media_id,:name, .... $query = sprintf($_query, $bindKeys, $bindPlaceHolders); } if (strlen($query) > 0 && !$mysql->execute($query, $bindArr)) { syncMedia($type); throw new Exception($mysql->error); } $returnArr['media_id'] = $response['media_id']; } } catch (Exception $ex) { log_error('API: media_upload', json_encode($file), $ex->getMessage()); $returnArr = array('code' => 500, 'err' => $ex->getMessage()); } die(json_encode($returnArr)); }
<?php require_once "../include/global.include.php"; if (isset($_GET['schema']) && isset($_GET['type'])) { $result = syncMedia($_GET['type']); print_r($result); }
} else { // update exist news records $content = array('media_id' => $media_id); // update wechat media foreach ($articles as $article) { $content['index'] = $article['record_index']; $content['articles'] = $article; $return = $wechat->updateMediaNews($content); if ($return['hasError']) { responseJSON($return['err']); } } // get the details $return = $wechat->getPermanentMedia($media_id, 'news'); if ($return['hasError']) { syncMedia('news'); // if failed, sync all news media once responseJSON($return['err']); } $data = $return['data']; // save thumb mapping $query = "INSERT INTO media_mapping ( media_id, media_type, filePath )\n SELECT ?, media_type, filePath FROM `media_mapping`\n WHERE media_id = ?\n ON DUPLICATE KEY UPDATE filePath = VALUES( filePath )"; $bindArr = $_bindArr = array(); foreach ($data as $key => $news) { array_push($bindArr, $news['thumb_media_id'], $articles[$key]['thumb_media_id']); $news['update_time'] = $update_time; array_push($_bindArr, $news['title'], $news['thumb_media_id'], $news['author'], $news['digest'], $news['show_cover_pic'], $news['content'], $news['content_source_url'], $update_time, $wechat->raw_json_encode($news), $media_id, $key); } if (!$mysql->execute($query, $bindArr)) { throw new Exception($mysql->error); }