问题
I am trying to send an http request from my server to another server in php. The url that I send the request to contains some utf8 characters for example http://www.aparat.com/etc/api/videoBySearch/text/نوروز .
Here is my code:
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);
}
I have also tried php-curl insted of file-get-contents but I got no answer.
回答1:
You just need to urlencode() the UTF-8 characters, like this:
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"
...
}
...
来源:https://stackoverflow.com/questions/37561402/php-curl-dosent-support-utf-8-in-url