簡體   English   中英

curl php 400錯誤請求

[英]400 Bad request with curl php

從其他站點或 API抓取數據我正在使用下面的代碼,但問題是當 URL 具有其他語言字體而不是其獲取錯誤時。 當 URL 為英文時,這不是問題

$url='https://techblogs.site/अगर-हम-इन-7-फीचर्स-को-2019-में-पा/';
function ftch($url,$post="", $ck = 'cookie.dat')
{
    $ver   = rand(4, 6);
    $agent = 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36';
    $ch    = curl_init();

    curl_setopt($ch, CURLOPT_URL, '' . $url . '');

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

    curl_setopt($ch, CURLOPT_REFERER, $url);

    curl_setopt($ch, CURLOPT_HEADER, true);

    curl_setopt($ch, CURLOPT_COOKIESESSION, 'cookie.s');
    //curl_setopt($ch, CURLOPT_PROXY, $proxyIP);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $ck);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $ck);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT,2);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept-Language: en-us,en;q=0.7,de-de;q=0.3',
        'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
     ));
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}

$content=ftch($url);
echo $content; 

您應該能夠對 URL(非 UTF-8 的部分)進行編碼,這將允許連接通過。

<?php
$url = 'https://techblogs.site/'.urlencode("अगर-हम-इन-7-फीचर्स-को-2019-में-पा");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM