Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURL: not fetching url with unicode characters

Tags:

php

curl

I'm using CURL to fetch a Yahoo! BOSS api url with a unicode query, but I'm getting an "bad request" error.

http://boss.yahooapis.com/ysearch/web/v1/கமல்ஹாசன்?appid={appid}&format=xml

The above url works fine and returns results in firefox.

Please could anybody help me to fix this.

$url = "http://boss.yahooapis.com/ysearch/web/v1/கமல்ஹாசன்?appid={appid}&format=xml";

$ch - curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data = curl_exec($ch);

curl_close($ch);
like image 740
Karthik Avatar asked Jul 07 '10 17:07

Karthik


1 Answers

you must urlencode your URL. follow the below codes:

$url = "http://yoursite.com/some-unicode-chars"

$path = parse_url($url, PHP_URL_PATH);

if ($path) {
    $segments = explode('/', $path);
    foreach($segments as $index => $segment) {
        $segments[$index] = urlencode($segment);
    }

    $url = str_replace($path, implode('/', $segments), $url);
}

// then use url in your curl command:
$handle = curl_init($url);
// ...
like image 56
Mahdi Avatar answered Nov 20 '22 10:11

Mahdi