PHP curl获取百度静态图乱码,请教高手们

cooldrw2012 2017-05-16 02:51:24



...全文
942 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingkong_zxy 2018-03-26
  • 打赏
  • 举报
回复
引用 15 楼 w6248117 的回复:
[quote=引用 14 楼 kingkong_zxy 的回复:] [quote=引用 13 楼 w6248117 的回复:] [quote=引用 12 楼 kingkong_zxy 的回复:] [quote=引用 10 楼 w6248117 的回复:] [quote=引用 8 楼 kingkong_zxy 的回复:] [quote=引用 6 楼 w6248117 的回复:] 这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。
写到img标签的src属性依旧乱码呀 [/quote] 能给我看下你的图片链接地址吗?[/quote] Php代码和楼主的基本一致,html代码是这样的 <img style="margin:20px" width="280" height="140" src="{:url('index/index')}"/>, 这是curl代码 function doCurl($url,$data=[],$type=0) { $ch=curl_init();//初始化一个curl对话并返回一个curl句柄 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//获取页面内容,不直接输出 curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_HEADER,0); if($type==1) { curl_setopt($ch,CURLOPT_POST,1);//type为1时post方式提交 curl_setopt($ch,CURLOPT_POSTFIELDS,$data);// } //执行curl $output=curl_exec($ch); //释放句柄 curl_close($ch); //返回执行结果 return $output; } 是因为文件流出问题了吗[/quote] 把你index方法也贴出来看看。还有你img标签的图片你给个截图吧![/quote] public static function getMapImage($center) { $data=[ 'ak'=>config('map.ak'), 'width'=>config('map.width'), 'height'=>config('map.height'), 'center'=>$center, 'markers'=>$center, ]; $url=config('map.baidu_map_url').config('map.staticimage')."?" .http_build_query($data); $res=doCurl($url); return $res; } 这是在扩展库中写的,在Index方法中调用 public function index() { return \Map::getMapImage("山东威海环翠区文化西路"); } 抱歉img这里一直上传图片失败[/quote] 你把return改成echo看看?我这边用return的话是得不到数据的。方法不会返回数据流[/quote] 我发现把文件保存到本地就可以实现了 public function map() { $return_content = \Map::getMapImage("山东威海环翠区文化西路山东大学"); $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm"; str_shuffle($str); $name=substr(str_shuffle($str),26,10); $dir_name='baidu map/'.$name.'.jpg'; $filename =$dir_name; $fp= @fopen($dir_name,"w"); fwrite($fp,$return_content); fclose($fp); // return $dir_name; } 至于上面为什么不能实现还是不明白;
kingkong_zxy 2018-03-26
  • 打赏
  • 举报
回复
引用 15 楼 w6248117 的回复:
[quote=引用 14 楼 kingkong_zxy 的回复:] [quote=引用 13 楼 w6248117 的回复:] [quote=引用 12 楼 kingkong_zxy 的回复:] [quote=引用 10 楼 w6248117 的回复:] [quote=引用 8 楼 kingkong_zxy 的回复:] [quote=引用 6 楼 w6248117 的回复:] 这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。
写到img标签的src属性依旧乱码呀 [/quote] 能给我看下你的图片链接地址吗?[/quote] Php代码和楼主的基本一致,html代码是这样的 <img style="margin:20px" width="280" height="140" src="{:url('index/index')}"/>, 这是curl代码 function doCurl($url,$data=[],$type=0) { $ch=curl_init();//初始化一个curl对话并返回一个curl句柄 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//获取页面内容,不直接输出 curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_HEADER,0); if($type==1) { curl_setopt($ch,CURLOPT_POST,1);//type为1时post方式提交 curl_setopt($ch,CURLOPT_POSTFIELDS,$data);// } //执行curl $output=curl_exec($ch); //释放句柄 curl_close($ch); //返回执行结果 return $output; } 是因为文件流出问题了吗[/quote] 把你index方法也贴出来看看。还有你img标签的图片你给个截图吧![/quote] public static function getMapImage($center) { $data=[ 'ak'=>config('map.ak'), 'width'=>config('map.width'), 'height'=>config('map.height'), 'center'=>$center, 'markers'=>$center, ]; $url=config('map.baidu_map_url').config('map.staticimage')."?" .http_build_query($data); $res=doCurl($url); return $res; } 这是在扩展库中写的,在Index方法中调用 public function index() { return \Map::getMapImage("山东威海环翠区文化西路"); } 抱歉img这里一直上传图片失败[/quote] 你把return改成echo看看?我这边用return的话是得不到数据的。方法不会返回数据流[/quote] exit也是,返回不到数据,必须在view层新建模板,然后return $this->fetch()将数据返回,模板中处理时通过a标签的href属性才可以查看到
木秀猿林 2018-03-26
  • 打赏
  • 举报
回复
引用 14 楼 kingkong_zxy 的回复:
[quote=引用 13 楼 w6248117 的回复:] [quote=引用 12 楼 kingkong_zxy 的回复:] [quote=引用 10 楼 w6248117 的回复:] [quote=引用 8 楼 kingkong_zxy 的回复:] [quote=引用 6 楼 w6248117 的回复:] 这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。
写到img标签的src属性依旧乱码呀 [/quote] 能给我看下你的图片链接地址吗?[/quote] Php代码和楼主的基本一致,html代码是这样的 <img style="margin:20px" width="280" height="140" src="{:url('index/index')}"/>, 这是curl代码 function doCurl($url,$data=[],$type=0) { $ch=curl_init();//初始化一个curl对话并返回一个curl句柄 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//获取页面内容,不直接输出 curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_HEADER,0); if($type==1) { curl_setopt($ch,CURLOPT_POST,1);//type为1时post方式提交 curl_setopt($ch,CURLOPT_POSTFIELDS,$data);// } //执行curl $output=curl_exec($ch); //释放句柄 curl_close($ch); //返回执行结果 return $output; } 是因为文件流出问题了吗[/quote] 把你index方法也贴出来看看。还有你img标签的图片你给个截图吧![/quote] public static function getMapImage($center) { $data=[ 'ak'=>config('map.ak'), 'width'=>config('map.width'), 'height'=>config('map.height'), 'center'=>$center, 'markers'=>$center, ]; $url=config('map.baidu_map_url').config('map.staticimage')."?" .http_build_query($data); $res=doCurl($url); return $res; } 这是在扩展库中写的,在Index方法中调用 public function index() { return \Map::getMapImage("山东威海环翠区文化西路"); } 抱歉img这里一直上传图片失败[/quote] 你把return改成echo看看?我这边用return的话是得不到数据的。方法不会返回数据流
kingkong_zxy 2018-03-26
  • 打赏
  • 举报
回复
引用 13 楼 w6248117 的回复:
[quote=引用 12 楼 kingkong_zxy 的回复:]
[quote=引用 10 楼 w6248117 的回复:]
[quote=引用 8 楼 kingkong_zxy 的回复:]
[quote=引用 6 楼 w6248117 的回复:]
这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。

写到img标签的src属性依旧乱码呀
[/quote]

能给我看下你的图片链接地址吗?[/quote]
Php代码和楼主的基本一致,html代码是这样的
<img style="margin:20px" width="280" height="140" src="{:url('index/index')}"/>,
这是curl代码
function doCurl($url,$data=[],$type=0)
{
$ch=curl_init();//初始化一个curl对话并返回一个curl句柄
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//获取页面内容,不直接输出
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);

if($type==1)
{
curl_setopt($ch,CURLOPT_POST,1);//type为1时post方式提交
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//
}

//执行curl
$output=curl_exec($ch);

//释放句柄
curl_close($ch);
//返回执行结果
return $output;
}
是因为文件流出问题了吗[/quote]

把你index方法也贴出来看看。还有你img标签的图片你给个截图吧![/quote]
public static function getMapImage($center)
{
$data=[
'ak'=>config('map.ak'),
'width'=>config('map.width'),
'height'=>config('map.height'),
'center'=>$center,
'markers'=>$center,
];
$url=config('map.baidu_map_url').config('map.staticimage')."?"
.http_build_query($data);

$res=doCurl($url);
return $res;
}
这是在扩展库中写的,在Index方法中调用
public function index()
{
return \Map::getMapImage("山东威海环翠区文化西路");
}
抱歉img这里一直上传图片失败
木秀猿林 2018-03-26
  • 打赏
  • 举报
回复
引用 12 楼 kingkong_zxy 的回复:
[quote=引用 10 楼 w6248117 的回复:] [quote=引用 8 楼 kingkong_zxy 的回复:] [quote=引用 6 楼 w6248117 的回复:] 这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。
写到img标签的src属性依旧乱码呀 [/quote] 能给我看下你的图片链接地址吗?[/quote] Php代码和楼主的基本一致,html代码是这样的 <img style="margin:20px" width="280" height="140" src="{:url('index/index')}"/>, 这是curl代码 function doCurl($url,$data=[],$type=0) { $ch=curl_init();//初始化一个curl对话并返回一个curl句柄 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//获取页面内容,不直接输出 curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_HEADER,0); if($type==1) { curl_setopt($ch,CURLOPT_POST,1);//type为1时post方式提交 curl_setopt($ch,CURLOPT_POSTFIELDS,$data);// } //执行curl $output=curl_exec($ch); //释放句柄 curl_close($ch); //返回执行结果 return $output; } 是因为文件流出问题了吗[/quote] 把你index方法也贴出来看看。还有你img标签的图片你给个截图吧!
kingkong_zxy 2018-03-26
  • 打赏
  • 举报
回复
引用 10 楼 w6248117 的回复:
[quote=引用 8 楼 kingkong_zxy 的回复:]
[quote=引用 6 楼 w6248117 的回复:]
这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。

写到img标签的src属性依旧乱码呀
[/quote]

能给我看下你的图片链接地址吗?[/quote]
Php代码和楼主的基本一致,html代码是这样的
<img style="margin:20px" width="280" height="140" src="{:url('index/index')}"/>,
这是curl代码
function doCurl($url,$data=[],$type=0)
{
$ch=curl_init();//初始化一个curl对话并返回一个curl句柄
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//获取页面内容,不直接输出
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);

if($type==1)
{
curl_setopt($ch,CURLOPT_POST,1);//type为1时post方式提交
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//
}

//执行curl
$output=curl_exec($ch);

//释放句柄
curl_close($ch);
//返回执行结果
return $output;
}
是因为文件流出问题了吗
木秀猿林 2018-03-26
  • 打赏
  • 举报
回复
引用 8 楼 kingkong_zxy 的回复:
[quote=引用 6 楼 w6248117 的回复:]
这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。

写到img标签的src属性依旧乱码呀
[/quote]



木秀猿林 2018-03-26
  • 打赏
  • 举报
回复
引用 8 楼 kingkong_zxy 的回复:
[quote=引用 6 楼 w6248117 的回复:] 这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。
写到img标签的src属性依旧乱码呀 [/quote] 能给我看下你的图片链接地址吗?
qq_39047451 2018-03-26
  • 打赏
  • 举报
回复
引用 8 楼 kingkong_zxy 的回复:
[quote=引用 6 楼 w6248117 的回复:] 这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。
写到img标签的src属性依旧乱码呀 [/quote] 将数据流写入文件
kingkong_zxy 2018-03-26
  • 打赏
  • 举报
回复
引用 6 楼 w6248117 的回复:
这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。
写到img标签的src属性依旧乱码呀
kingkong_zxy 2018-03-26
  • 打赏
  • 举报
回复
老哥你是怎么解决的啊
木秀猿林 2018-03-26
  • 打赏
  • 举报
回复
这个不是乱码!是文件流,你把这个写入的到图片文件,或者之间通过html的img标签显示到浏览器中,应该就是一张图片了。
xuzuning 2018-03-25
  • 打赏
  • 举报
回复
1、确认没有BOM 头或其他非图片数据 从截图上看应该没有 2、输出正确的数据类型声明 从代码上看,的确没有
fruitlin 2017-05-23
  • 打赏
  • 举报
回复
这就是你想要的图片数据,只不过现在是字符串形式,你得新建文件,改成 图片后缀,你就能看到了。
「已注销」 2017-05-23
  • 打赏
  • 举报
回复
二进制。。二进制。。二进制。。二进制。。二进制。。二进制。。二进制。。二进制。。二进制。。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧