另一款百度短网址生成 PHP 代码

4,063 0

今天早上我发了一篇利用百度短网址 API 生成短网址的教程和代码,然后在 CSDN 发现了另一个更好的生成百度短网址方案,于是就想转发过来分享给小伙伴们,也可以和我上一篇文章的版本进行一下对比,反正我是觉得它的比较快速方便,设计逻辑也很精密,下面是代码部分。

PHP 代码

PHP
<?php
function send_post($url, $post_data) {
    $postdata = http_build_query($post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 15 * 60
        )  
    );  
    $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);
echo $arr['tinyurl'];
?>

评论

(0)

暂无评论,来说两句吧

发布评论

© 2026 一纸忘忧. 保留所有权利.

一纸忘忧的个人网站,关于技术探索与日常生活,记录 Web 开发、服务端与 DevOps 实践,分享软件工具、数码体验,以及代码之外的见闻与思考。