当前位置:首页 > PHP > 正文内容

达摩-营销素材自动上传接口备份代码

高老师8个月前 (02-24)PHP284

上传图片代码:

/**
 * uploadImage
 * @param $imagePath
 * @return mixed
 * @throws BusinessException
 */
public function uploadImage($imagePath)
{
    echo '正在上传图片' . $imagePath . PHP_EOL;
    $this->checkCookie();
    $baseInfo = pathinfo($imagePath);
    $url = 'https://hope.demogic.com/api-marketing/content/upload-image?wxFlag=1&requestProject=marketing';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    $headers = array(
        'Accept: application/json, text/plain, */*',
        'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
        'Connection: keep-alive',
        'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryjIc86qFLxj5NjvGi',
        'Cookie: ' . $this->cookie,
        'Origin: https://hope.demogic.com',
        'Referer: https://hope.demogic.com/content-center/',
        'Sec-Fetch-Dest: empty',
        'Sec-Fetch-Mode: cors',
        'Sec-Fetch-Site: same-origin',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
        'channelEntrance: merchant_backstage',
        'sec-ch-ua: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
        'sec-ch-ua-mobile: ?0',
        'sec-ch-ua-platform: "Windows"'
    );
    $data = array(
        'file' => new CURLFile($imagePath, 'image/jpeg', $baseInfo['basename']),
        'uuid' => '7a763df7-5723-451e-b672-caa7ba7c057e'
    );
    $response = curl($url, $data, false, $headers, 3, 1);
    if (!$response) {
        throw new BusinessException('请求达摩上传文件接口失败');
    }
    $responseData = json_decode($response, true);
    if (!$responseData) {
        throw new BusinessException('转换达摩上传文件接口数据失败');
    }
    $this->checkLogin($responseData);
    return $responseData;
}

发布素材接口:

/**
 * 发布素材
 * @return array
 */
public function saveMaterial($materialTitle, $materialImageSource, $contentGoodsIdList, $thirdGoodsList, $contentCategoryId, $producerId)
{
    $this->checkCookie();

    // 提取全部图片
    $materialImageUrls = array_column($materialImageSource, 'url');

    // 提取全部图片配置
    $materialImageInfos = [];
    foreach ($materialImageSource as $value) {
        unset($value['url']);
        $materialImageInfos[] = json_encode($value, 256);
    }
    $materialImageInfosText = join('|', $materialImageInfos);

    // 请求地址
    $url = 'https://hope.demogic.com/api-marketing/content/save-material?requestProject=marketing';
    $data = array(
        'pageId' => '',
        'shareLinkFlag' => 1,
        'materialSubTitle' => '',
        'materialLink' => '',
        'materialFirstImageUrl' => '',
        'additionalList' => array(),
        'materialPurpose' => 1,
        'contentMaterialId' => '',
        'materialTitle' => $materialTitle, //标题
        'materialType' => 3,
        'shelfStatus' => 1,
        'publishType' => 1,  // 0.暂不发布 1.发布
        'publishBeginTime' => '',
        'publishEndTime' => '',
        'materialContent' => '',
        'materialImageUrls' => join(',', $materialImageUrls), // 图片地址逗号隔开
        'materialThumbnailImageUrls' => join(',', $materialImageUrls), // 图片地址逗号隔开,
        'materialVideoUrl' => '',
        'materialVideoImageUrl' => '',
        'materialVideoFirstImageUrl' => '',
        'ruleId' => '',
        'contentTagIdList' =>
            array(
                0 => '509419425271668780', //标签-固定为其他内容
            ),
        'contentColumnIdList' =>
            array(),
        'contentCategoryId' => $contentCategoryId, //分类编号
        'contentGoodsIdList' => $contentGoodsIdList, //关联商品编号
        'duration' => '',
        'producerId' => $producerId, //发布人编号
        'materialImageInfos' => $materialImageInfosText,
        'materialVideoInfo' => '',
        'materialVideoImageInfo' => '',
        'materialVideoFirstImageInfo' => '',
        'materialFirstImageInfo' => '',
        'producer' => NULL,
        'thirdGoodsList' => $thirdGoodsList
    );

    $headers = array();
    $headers[] = 'Accept: application/json, text/plain, */*';
    $headers[] = 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8';
    $headers[] = 'Connection: keep-alive';
    $headers[] = 'Content-Type: application/json;charset=UTF-8';
    $headers[] = 'Cookie:' . $this->cookie;
    $headers[] = 'Origin: https://hope.demogic.com';
    $headers[] = 'Referer: https://hope.demogic.com/content-center/';
    $headers[] = 'Sec-Fetch-Dest: empty';
    $headers[] = 'Sec-Fetch-Mode: cors';
    $headers[] = 'Sec-Fetch-Site: same-origin';
    $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
    $headers[] = 'channelEntrance: merchant_backstage';
    $headers[] = 'sec-ch-ua: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"';
    $headers[] = 'sec-ch-ua-mobile: ?0';
    $headers[] = 'sec-ch-ua-platform: "Windows"';
    $response = curl($url, json_encode($data), false, $headers, 1, 0);
    if (!$response) {
        throw new BusinessException('请求达摩发布素材接口失败,接口未响应数据');
    }
    $responseData = json_decode($response, true);
    if (!$responseData) {
        throw new BusinessException('请求达摩发布素材接口失败,接口返回非Json数据');
    }
    $this->checkLogin($responseData);
    return $responseData;
}


扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/878.html

分享给朋友:

“达摩-营销素材自动上传接口备份代码” 的相关文章

Cookie禁用了Session还能用吗?

Cookie禁用了Session还能用吗?

    Session在默认情况下是使用客户端的Cookie来保存Session ID的,所以当客户端的cookie出现问题的时候就会影响Session了,如果你单纯的认为SESSION和COOKIE没有联系的话请迅速百度补脑。  &nbs...

php 经纬度获取城市,php 经纬度转城市

php 经纬度获取城市,php 经纬度转城市

通过经纬度转换为城市名称,并获得城市的编号,通常在全国类型的商城中比较通用。//经纬度转城市名称(返回城市id,城市名称) //$ak开发密钥,$lat纬度,$lng经度,$type返回数据类型 public function getCityName($ak,$lat,$lng)...

 php 判断是否post,php判断是否post提交,php 判断是否为post,php 判断get 还是post

php 判断是否post,php判断是否post提交,php 判断是否为post,php 判断get 还是post

if($_SERVER['REQUEST_METHOD'] == 'POST') { echo('This is  post '); } elseif ($_SERVER['...

php多进程,php多进程处理任务,php多进程应用场景

php多进程,php多进程处理任务,php多进程应用场景

php多进程应用场景主要是非web端,fpm下是不支持多进程的,非类linux操作系统都不支持,请在cli模式使用.可以使用多进程做任务分发,批量计算,批量文件处理,批量爬虫,网络运维等等。下面看一份简单的入门demo//创建子进程 $pid=pcntl_fork(); //返回-1,创建失败,不...

Thinkphp Call Stack,PHP调用栈Call Stack的获取

Thinkphp Call Stack,PHP调用栈Call Stack的获取

ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...

php多进程实现任务管理器,定时执行任务,支持守护

php多进程实现任务管理器,定时执行任务,支持守护

主要原理是通过PHP创建多个子进程,在子进程中发送进程闹钟信号,然后再监听闹钟信号中继续发送闹钟信号。同时通过父进程设置非阻塞运行。代码如下:<?php /**  * 订单任务  */ class Order {    &n...