file_get_contents post提交一则


提交数据,获取所有t.cn短域名网址

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);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM