0

to scrape data from other site or API I am using below code but problem is that when URL has other languages font than its getting error. it's not a problem when URL having the English language

$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; 
user3601901
  • 1
  • 1
  • 2
  • Does this answer your question? [php-curl dosen't support utf-8 in url](https://stackoverflow.com/questions/37561402/php-curl-dosent-support-utf-8-in-url) – Patrick Q Dec 10 '19 at 19:24

1 Answers1

0

You should be able to encode the URL (the parts that are not UTF-8) and that will allow the connection to get through.

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