调用百度api 生成短网址

来源:互联网 发布:舆情软件 编辑:程序博客网 时间:2024/04/27 10:54

功能描述:
自己有一个长网址,调用API直接做成一个短的,就不用去百度的短网址链接了。

       function send_post($url, $post_data) {          $postdata = http_build_query($post_data);          $options = array(  //必要的http请求信息          'http' => array(             'method' => 'POST',             'header' => 'Content-type:application/x-www-form-urlencoded',             'content' => $postdata,             'timeout' => 15 * 60 // 超时时间(单位:s)          )        );          $context = stream_context_create($options);          $result = file_get_contents($url, false, $context);          return $result;        }          //使用方法          $post_data = array(            'url' => 'URL'        );          $json_data =  send_post('http://dwz.cn/create.php', $post_data);          $arr=json_decode($json_data,true);//由于返回的数据是json,需要解码一下        echo $arr['tinyurl'];//输出短网址字段即可
0 1
原创粉丝点击