获取url返回值时出现中文乱码

正常访问url

获取url返回值时出现中文乱码0

php获取url

获取url返回值时出现中文乱码1

我的代码

$ch = curl_init();
$timeout = 5;
curl_setopt($ch, curlopt_encoding, “gzip”);
curl_setopt ($ch, curlopt_url, ‘*****’);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;

回复讨论(解决方案)

看形势,用 utf8_decode 解一下码就可以了

最好给出真实的 url 验证一下

看形势,用 utf8_decode 解一下码就可以了

最好给出真实的 url 验证一下

url:http://sandbox.api.simsimi.com/request.p?key=2b0ffb43-e90b-4ff5-96c8-149b1af14f0b&lc=zh&ft=1.0&text=我是

完整代码

{“response”:”盲潞潞氓庐露忙聹聣忙聺聨猫碌芦氓庐掳盲潞聠”,”id”:”14514032″,”result”:100,”msg”:”ok.”}

{“response”:”盲潞潞氓庐露忙聹聣忙聺聨猫碌芦氓庐掳盲潞聠”,”id”:”14514032″,”result”:100,”msg”:”ok.”}

response的值是乱码

http://sandbox.api.simsimi.com/request.p?key=2b0ffb43-e90b-4ff5-96c8-149b1af14f0b&lc=zh&ft=1.0&text=我是

直接访问这个url时,response返回的值是正常的

获取url返回值时出现中文乱码2

curl::get 是这样定义的
class curl {
public static function get($durl, $data=array()) {
$cookiejar = realpath(‘cookie.txt’);
$t = parse_url($durl);
$ch = curl_init();
curl_setopt($ch, curlopt_url,$durl);
curl_setopt($ch, curlopt_timeout,5);
curl_setopt($ch, curlopt_ssl_verifypeer, 0);
curl_setopt($ch, curlopt_useragent, $_server[‘http_user_agent’]);
curl_setopt($ch, curlopt_referer, “http://$t[host]/”);
curl_setopt($ch, curlopt_cookiefile, $cookiejar);
curl_setopt($ch, curlopt_cookiejar, $cookiejar);
curl_setopt($ch, curlopt_returntransfer,1);
curl_setopt($ch, curlopt_encoding, 1); //gzip 解码
curl_setopt($ch, curlopt_followlocation, true);
if($data) {
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $data);
}
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
}
显然你少了点什么。你可在此基础上做些删减,看看是少了什么

$text = urlencode(‘我是’);
$ch = curl_init();
$timeout = 5;
$url = ‘http://sandbox.api.simsimi.com/request.p?key=2b0ffb43-e90b-4ff5-96c8-149b1af14f0b&lc=zh&ft=1.0&text=’.$text;
curl_setopt($ch, curlopt_encoding, 1);
curl_setopt ($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_returntransfer, true);
curl_setopt($ch, curlopt_useragent, $_server[‘http_user_agent’]);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;

这样就可以了

{“response”:”尊貴的暗黑精靈女皇陛下”,”id”:”42235075″,”result”:100,”msg”:”ok.”}

嗯,仅缺少 curl_setopt($ch, curlopt_useragent, $_server[‘http_user_agent’]); 而已

curl_setopt($ch, curlopt_encoding, “gzip”); 是多余的

$text = urlencode(‘我是’);
$ch = curl_init();
$timeout = 5;
$url = ‘http://sandbox.api.simsimi.com/request.p?key=2b0ffb43-e90b-4ff5-96c8-149b1af14f0b&lc=zh&ft=1.0&text=’.$text;
curl_setopt($ch, curlopt_encoding, 1);
curl_setopt ($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_returntransfer, true);
curl_setopt($ch, curlopt_useragent, $_server[‘http_user_agent’]);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;

这样就可以了

{“response”:”尊貴的暗黑精靈女皇陛下”,”id”:”42235075″,”result”:100,”msg”:”ok.”}

获取url返回值时出现中文乱码3

一样的代码,我这边运行多次还是出现乱码。直接访问不会出现乱码

只有google浏览器是可以的

获取url返回值时出现中文乱码4

// echo 前
header(‘content-type: text/html; charset=utf-8’);
echo …

curl 我推荐你使用我的 modphp 框架,当然你可以把它当插件来使用,不大,200kb,里面内置了非常强大的 curl 支持,自动解码什么的,都不在话下,链接 http://modphp.hyurl.com/ ,或者直接百度吧

Posted in 未分类

发表评论