file_get_contents 和curl的使用

file_get_contents 方式
<?php
function Post($url, $post = null) {
    if (is_array($post)) {
        ksort($post);
        $content = http_build_query($post);
        $content_length = strlen($content);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' =>
                "Content-type: application/x-www-form-urlencoded\r\n" .
                "Content-length: $content_length\r\n",
                'content' => $content
            )
        );
        return file_get_contents($url, false, stream_context_create($options));
    }
}
 
$data = array
    (
    'url' => 'http://www.waqiang.com/index.php/url/shorten',
    'submit' => 'submit',
);
 
$response = Post('http://www.waqiang.com/index.php/url/shorten', $data);
$reg = '#[\'"](http:(//|\\/\\/)t\.cn((/|\\/)([^\'"/]+)(/|\\/)?|(/|\\/)))[\'"]#';
preg_match_all($reg, $response, $match);
var_dump($match);

 

curl 方式
<?php
function shorturl($long_url) {
    $url = 'http://www.waqiang.com/index.php/url/shorten';
    $data = array(
        'url' => $long_url,
        'submit' => 'Submit'
    );
    $curlObj = curl_init();
    $options = array(
        CURLOPT_URL => $url,
        CURLOPT_POST => TRUE, //使用post提交
        CURLOPT_RETURNTRANSFER => TRUE, //接收服务端范围的html代码而不是直接浏览器输出
        CURLOPT_TIMEOUT => 4,
        CURLOPT_POSTFIELDS => http_build_query($data), //post的数据
    );
    curl_setopt_array($curlObj, $options);
    $response = curl_exec($curlObj);
    curl_close($curlObj);
    return $response;
}
 
$result = shorturl('http://www.waqiang.com/index.php/url/shorten');
$reg = '#[\'"](http:(//|\\/\\/)t\.cn((/|\\/)([^\'"/]+)(/|\\/)?|(/|\\/)))[\'"]#';
preg_match_all($reg, $result, $match);
var_dump($match);

 

 

出处:http://www.cnblogs.com/quinnxu/p/3493266.html

转载于:https://www.cnblogs.com/guobingbing/p/4283880.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值