InquiryRelayService.php 5.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2025/2/13
 * Time: 11:01
 */

namespace App\Services;

use Illuminate\Support\Facades\Log;

/**
 * Class InquiryRelayService
 * @package App\Services
 */
class InquiryRelayService
{
    /**
     * 获取询盘
     * @return mixed|string
     */
    public function getInquiryRelay()
    {
        $date = date('Y-m-d');
        $token = md5($date . 'qqs');
        $url = 'https://www.globalso.site/api/external-interface/echo_inquriy/d1483a8e57cb485a?date=' . $date . '&token=' . $token . '&type=2';
        $result = http_get($url);
        return $result;
    }

    /**
     * 获取询盘
     * @param $id
     * @return array|bool
     */
    public function getInquirySzcm($id)
    {
        try {
            // 获取数据
            $url = "https://api.szcmapi.com/get_inquiry.aspx?id=" . $id;
            $json = $this->szcmCurl($url);
            // 兼容过去到的数据, 比较乱
            $json = trim(str_replace("\r\n", '\n', (string)$json));
            $json = str_replace('3/4"', '3/4', $json);
            $json = str_replace('"Steklopribor"', 'Steklopribor', $json);
            $json = str_replace('"Net30 days"', 'Net30 days', $json);
            $json = trim($json);
            $json = str_replace("\n", '', $json);
            $array = json_decode($json, true);

            if (empty($array))
                return false;

            // 整合最终数据
            $title = base64_decode($array['title']);
            $title = str_replace("'", '', $title);
            $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
            $title = str_replace("'", '', $title);
            $message = html_entity_decode(addslashes(base64_decode($array['Message'])), ENT_QUOTES, 'UTF-8');
            $message = str_replace("'", '', $message);
            $email = trim($array['Email']);
            $name = html_entity_decode($array['Name'], ENT_QUOTES, 'UTF-8');
            $name = str_replace("'", '', $name);

            $result = [
                'image' => $array['image'] ?: '',
                'time' => $array['submit_time'] ?: date('Y-m-d H:i:s'),
                'name' => $name,
                'email' => $email,
                'phone' => $array['Phone'] ?: '',
                'refer' => $array['refer'] ?: '',
                'message' => $message,
                'ip' => $array['submit_ip'] ?: '',
                'source_address' => 'api.szcmapi.com',
                'title' => $title,
                'submit_country' => $array['submit_country'] ?: '',
                'origin_key' => $array['Id'],
            ];
            return $result;
        } catch (\Exception $e) {
            Log::error('获取询盘: getInquirySzcm : ' . $id . ', error: ' . $e->getMessage());
            return false;
        }
    }

    /**
     * @param $url
     * @return bool|string
     */
    public function szcmCurl($url)
    {
        $agent = 'ChuangMao_API_Bot';
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSLVERSION, 'all');
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    /**
     * 获取findsupply询盘
     * @param $url
     * @return array
     * @throws \Exception
     * @author Akun
     * @date 2025/03/04 10:48
     */
    public function getInquiryFs($url)
    {
        $re = curl_get($url);
        $result = [];
        $next_page_url = '';
        if (isset($re['status']) && $re['status'] == 200) {
            $next_page_url = $re['data']['next_page_url'];
            foreach ($re['data']['data'] as $v) {
                $dateTime = new \DateTime($v['created_at']);
                $time = $dateTime->format('Y-m-d H:i:s');
                $result[] = [
                    'time' => $time,
                    'name' => $v['NAME'] ?: '',
                    'email' => $v['email'] ?: '',
                    'phone' => $v['phone_number'] ?: '',
                    'refer' => $v['page_url'] ?: '',
                    'message' => $v['content'] ?: '',
                    'ip' => $v['ip'] ?: '',
                    'source_address' => 'www.findsupply.com',
                    'title' => $v['products_title'] ?: '',
                    'submit_country' => $v['country'] ?: ''
                ];
            }
        }

        return ['next_page_url' => $next_page_url, 'data' => $result];
    }

########################################################################################################################
#                                               询盘结束, 同步项目及路由                                                  #
########################################################################################################################
}