首页 > Web开发 > 详细

基于php curl以post方式请求数据

时间:2014-11-26 14:34:42      阅读:303      评论:0      收藏:0      [点我收藏+]
<?php     

    $url = ‘http://127.0.0.1/test.php‘;    //接口地址 
    $data = array(     
        ‘key1‘=>‘value1‘,
        ‘key2‘=>‘value2‘
    );     

    $json_str = curl_post($url, $data);     
    $json_arr = json_decode($json_str, TRUE);     
    var_dump($json_arr);     

    function curl_post($url, $data) {     
        $ch = curl_init();     
        $timeout = 300;      
        curl_setopt($ch, CURLOPT_URL, $url);                              // 请求地址
        curl_setopt($ch, CURLOPT_REFERER, "http://www.jianbin.info/ ");   // 请求来源地址   
        curl_setopt($ch, CURLOPT_POST, true);                             // POST请求方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                      // 提交参数
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                      // 以文件流的形式返回
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);               // 请求超时时间  
        $result = curl_exec($ch);     
        curl_close($ch);     
        return $result;     
    }




基于php curl以post方式请求数据

原文:http://my.oschina.net/u/2269208/blog/348910

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!