InquiryRelayService.php 3.9 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;
    }

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