<?php
/**
  * @method 百度
  * @author soul
  */
class OthBaidu
{
	/**
	 * @method 短网址
	 * @param  
	*/
	public function dwz($urlArr)
	{
		$arr = [];
		$urlArr = array_unique($urlArr);
		if(!empty($urlArr))
		{
			$data = [];
			foreach($urlArr as $turl)
			{
				$data[] = ["LongUrl"=>$turl, "TermOfValidity"=>"long-term"];
			}
			$result = $this->curl('https://dwz.cn/api/v3/short-urls', $data);
			if($result['Code'] == '0')
			{
				foreach($result['ShortUrls'] as $v)
				{
					$arr[$v['LongUrl']] = $v['ShortUrl'];
				}
			}
		}
		return LibFc::ReturnData(true, $arr);
	}

	public function curl($url, $data = [])
	{
		$data = json_encode($data);
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, ['Dwz-Token:e03519b5d6451eb01d7cbf616a68bdd0', 'Content-Type:application/json; charset=UTF-8']);
		$result = curl_exec($ch);
		return json_decode($result, true);
	}
}