您的位置:首页 > 编程语言 > PHP开发

curl PHP 脚本例子

2015-09-20 18:59 821 查看
上网查了写资料写了个自己需要的PHP Curl函数,调用的是百度API获取数据:function getData($add) {

  $post_data = array( 'ak=kkkk', 'callback=renderOption', 'output=json', 'city=XXX, 'address='.$add ); //传输的参数拼装

  //print_r($post_data);

  $post_data = implode('&',$post_data);

  $url='http://api.map.baidu.com/geocoder/v2/?'; //路由拼装,你需要的参数就不需要再放到这里,上面拼装好就行。

  //下面是curl的一些初始化,这里看PHP手册。

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_POST, 1);

  curl_setopt($ch, CURLOPT_URL,$url);

  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

  ob_start();

  curl_exec($ch); //执行curl

  $result = ob_get_contents() ; // 获取内容

  ob_end_clean();

  return json_decode($result)->result->location;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: