用CURL获取短连接

来源:互联网 发布:json是用来干嘛的 编辑:程序博客网 时间:2024/04/30 19:00

        对CURL很感兴趣,所以做了一些尝试。以下代码可以调用第三方工具把我们的URL转为短连接(微博中的短连接)

<?phpextract($_POST);$url = 'http://www.waqiang.com/index.php/url/shorten';//调用转换地址$fields = array(         'url'=>urlencode('http://www.goutuijian.com'), //POST数组); foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; } //对POST中字段进行转码rtrim($fields_string ,'&') ; $ch = curl_init() ; //初始化curl_setopt($ch, CURLOPT_URL,$url) ; curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST,count($fields)) ; curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ; //execute post $result = curl_exec($ch);preg_match_all("/\<input name=\"url\" id=\"url\" type=\"text\" class=\"input\" readonly=\"readonly\" value=\"(?<url>.*)\" \/\>/", $result, $ok);echo $ok['url'][0];//这就是得到的短连接//close connection curl_close($ch) ; ?>


原创粉丝点击