php-curl在网址中不支持utf-8 [英] php-curl dosen't support utf-8 in url

查看:44
本文介绍了php-curl在网址中不支持utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将HTTP请求从我的服务器发送到php中的另一台服务器.我发送请求的网址包含一些utf8字符,例如

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/نوروز .

这是我的代码:

 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-curl插入文件获取内容,但是我没有答案.

I have also tried php-curl insted of file-get-contents but I got no answer.

推荐答案

您只需要

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"
      ...
    }
    ...

这篇关于php-curl在网址中不支持utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆