🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# php curl ``` <pre class="calibre29">``` <span class="token6">/** * 请求ts返回头 */</span> <span class="token2">function</span> <span class="token3">gettsheard</span><span class="token1">(</span>$<span class="token5">URL</span><span class="token1">)</span><span class="token1">{</span> $<span class="token5">URL</span> <span class="token">=</span> <span class="token3">preg_replace</span><span class="token1">(</span><span class="token4">'/\s/'</span><span class="token1">,</span> <span class="token4">''</span><span class="token1">,</span> $<span class="token5">URL</span><span class="token1">)</span><span class="token1">;</span> $url_pars<span class="token">=</span> <span class="token3">parse_url</span><span class="token1">(</span>$<span class="token5">URL</span><span class="token1">)</span><span class="token1">;</span> $header<span class="token">=</span><span class="token3">array</span><span class="token1">(</span><span class="token4">'Accept:*/*'</span><span class="token1">,</span> <span class="token4">'Accept-Language:zh-CN,zh;q=0.9,ja;q=0.8'</span><span class="token1">,</span> <span class="token4">'Content-Type:application/x-www-form-urlencoded'</span><span class="token1">,</span> <span class="token4">'Host:'</span><span class="token1">.</span>$url_pars<span class="token1">[</span><span class="token4">'host'</span><span class="token1">]</span> <span class="token1">.</span> <span class="token1">(</span>$url_pars<span class="token1">[</span><span class="token4">'port'</span><span class="token1">]</span><span class="token">==</span><span class="token5">80</span> <span class="token">?</span> <span class="token4">''</span> <span class="token1">:</span> <span class="token4">':'</span><span class="token1">.</span>$url_pars<span class="token1">[</span><span class="token4">'port'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">,</span> <span class="token6">//'Range: bytes=0-3',</span> <span class="token4">'Upgrade-Insecure-Requests:1'</span><span class="token1">)</span><span class="token1">;</span> <span class="token">/</span><span class="token">/</span><span class="token3">dump</span><span class="token1">(</span>$<span class="token5">URL</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">/**解决香港卫视 拒绝curl方式*/</span> <span class="token6">//$header[] = "Accept: application/json";</span> <span class="token6">// $header[] = "Accept-Encoding: gzip";</span> <span class="token6">/**解决香港卫视 */</span> $ch <span class="token">=</span> <span class="token3">curl_init</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_RETURNTRANSFER</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_CONNECTTIMEOUT</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_TIMEOUT</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSL_VERIFYPEER</span><span class="token1">,</span> <span class="token5">false</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSL_VERIFYHOST</span><span class="token1">,</span> <span class="token5">false</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSLVERSION</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_URL</span><span class="token1">,</span> $<span class="token5">URL</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_USERAGENT</span><span class="token1">,</span> <span class="token4">"Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">/**解决香港卫视 */</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_HTTPHEADER</span><span class="token1">,</span> $header<span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_ENCODING</span><span class="token1">,</span> <span class="token4">"gzip"</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');</span> <span class="token6">/**解决香港卫视 */</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_HEADER</span><span class="token1">,</span> <span class="token5">true</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//返回 header 部分</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_NOBODY</span><span class="token1">,</span> <span class="token5">true</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//设定是否不输出页面内容</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_FOLLOWLOCATION</span><span class="token1">,</span> <span class="token5">true</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//允许重定向</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_AUTOREFERER</span><span class="token1">,</span> <span class="token5">true</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//当Location:重定向时,自动设置header中的Referer:信息</span> $data <span class="token">=</span> <span class="token3">curl_exec</span><span class="token1">(</span>$ch<span class="token1">)</span><span class="token1">;</span> <span class="token6">// $err= curl_error($ch);</span> $code<span class="token">=</span> <span class="token3">curl_getinfo</span><span class="token1">(</span>$ch<span class="token1">,</span><span class="token5">CURLINFO_HTTP_CODE</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_close</span><span class="token1">(</span>$ch<span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>$code<span class="token">>=</span><span class="token5">200</span> <span class="token">&&</span> $code<span class="token"><</span><span class="token5">400</span><span class="token1">)</span> <span class="token1">{</span> <span class="token6">//正常</span> $status <span class="token">=</span> <span class="token5">1</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">empty</span><span class="token1">(</span>$code<span class="token1">)</span><span class="token1">)</span><span class="token1">{</span> <span class="token6">//正常</span> $status <span class="token">=</span> <span class="token">-</span><span class="token5">1</span><span class="token1">;</span> <span class="token6">//var_dump($URL); var_dump($code);var_dump($data);var_dump($err);</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> <span class="token6">//异常</span> $status <span class="token">=</span> <span class="token5">0</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">return</span> $status<span class="token1">;</span> ``` ``` ``` <pre class="calibre14">``` $<span class="token5">URL</span> <span class="token">=</span> <span class="token3">preg_replace</span><span class="token1">(</span><span class="token4">'/\s/'</span><span class="token1">,</span> <span class="token4">''</span><span class="token1">,</span> $<span class="token5">URL</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//dump($URL);</span> $ch <span class="token">=</span> <span class="token3">curl_init</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_RETURNTRANSFER</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_CONNECTTIMEOUT</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_TIMEOUT</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSL_VERIFYPEER</span><span class="token1">,</span> <span class="token5">false</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSL_VERIFYHOST</span><span class="token1">,</span> <span class="token5">false</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_SSLVERSION</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_URL</span><span class="token1">,</span> $<span class="token5">URL</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//curl_setopt($ch, CURLOPT_HEADER, true); //不返回 header 部分</span> <span class="token6">//curl_setopt($ch, CURLOPT_NOBODY, true); //设定是否不输出页面内容</span> <span class="token3">curl_setopt</span><span class="token1">(</span>$ch<span class="token1">,</span> <span class="token5">CURLOPT_USERAGENT</span><span class="token1">,</span> <span class="token4">"Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);</span> $data <span class="token">=</span> <span class="token3">curl_exec</span><span class="token1">(</span>$ch<span class="token1">)</span><span class="token1">;</span> <span class="token6">//$err = curl_error($ch);</span> <span class="token3">curl_close</span><span class="token1">(</span>$ch<span class="token1">)</span><span class="token1">;</span> <span class="token6">//dump($data);</span> <span class="token6">//dump($err);</span> <span class="token2">return</span> $data<span class="token1">;</span> ``` ```