curl代码如下:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);//设置成1可以得到header的信息
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
$headers = array();
$headers[] = 'Accept: application/json, text/javascript, */*; q=0.01';
$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,fr;q=0.7,zh-TW;q=0.6,ja;q=0.5';
$headers[] = 'Connection: keep-alive';
$headers[] = 'Content-Type: application/json;charset=UTF-8';
$headers[] = 'Host: ' . str_replace('http://', '', $this->base_url);
$headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36';
$headers[] = 'Cookie: ' . $ck;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 把post的变量加上
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //对于重定向非常有用的东西,自动寻找路径,避免重定向循环
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$output = curl_exec($ch);
$info=curl_getinfo($ch);
返回数据乱码,如下:^ b"\x1Fï\x08\x00\x00\x00\x00\x00\x00\x03\x01(\x00Î {"code":1,"msg":"Õ¢ôÕë쵩©µêŵ£¬Õ╝Çþøÿ"}\x04ÚÆ\(\x00\x00\x00"
解决办法:
curl加入如下参数:
curl_setopt($curl_handle, CURLOPT_ENCODING, "identity");
得到正确返回结果:
^ "{"code":1,"msg":"当前游戏未开盘"}"
发表评论 取消回复