2016-06-01 64 views
2

我试图从我的服务器发送一个http请求到另一台服务器。 我发送请求的URL包含一些utf8字符,例如http://www.aparat.com/etc/api/videoBySearch/text/نوروزphp-curl dosen't在url中支持utf-8

这里是我的代码:

const api_adress = 'http://www.aparat.com/etc/api/'; 
const xml_config = 'http://www.aparat.com/video/video/config/videohash/[ID]/watchtype/site'; 

public function getDataInArray($JSON_ADRESS) 
{ 
    $json = json_decode(file_get_contents(self::api_adress . utf8_encode($JSON_ADRESS)), true); 
    return ($json != NULL) ? $json : die(NULL); 
} 

我也曾尝试PHP卷曲insted的文件-GET-的内容,但我没有答案。

+0

的可能的复制[文件\ _get \ _contents()打破了UTF-8字符(http://stackoverflow.com/questions/2236668/file-get-contents-breaking-up-utf-8-characters) – ksno

+0

不,它不是重复的。 – Will

+0

我看你已经试过[utf8_encode()](http://php.net/)。该函数从ISO-8859-1转换为UTF-8,并且不能在ISO-8859-1中存储阿拉伯字符。 –

回答

1

你只需要urlencode()的UTF-8字符,像这样:

php > $api = 'http://www.aparat.com/etc/api/'; 
php > $searchEndpoint = 'videoBySearch/text/'; 

php > var_dump($api . $searchEndpoint . urlencode('نوروز')); 
string(79) "http://www.aparat.com/etc/api/videoBySearch/text/%D9%86%D9%88%D8%B1%D9%88%D8%B2" 

php > $encodedUrl = $api . $searchEndpoint . urlencode('نوروز'); 
php > var_dump($encodedUrl); 

string(79) "http://www.aparat.com/etc/api/videoBySearch/text/%D9%86%D9%88%D8%B1%D9%88%D8%B2" 
php > var_dump(json_decode(file_get_contents($encodedUrl))); 
object(stdClass)#1 (2) { 
    ["videobysearch"]=> 
    array(20) { 
    [0]=> 
    object(stdClass)#2 (17) { 
     ["id"]=> 
     string(7) "4126322" 
     ["title"]=> 
     string(35) "استقبال از نوروز 1395" 
     ["username"]=> 
     string(6) "tabaar" 
     ... 
    } 
    ...