码迷,mamicode.com
首页 > Web开发 > 详细

>>fun:httpPost/httpGET>>功能:向$url POST/GET 数据

时间:2015-05-01 10:45:58      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:curl类运用   采集函数   post数据   get数据   

>>>函数名:httpPost

功能:向$url POST xml数据

code:

/**
 * [httpPost  向$url POST $data数据]
 * @param  [sting] $url  [action url]
 * @param  [array] $data [数据数组 eg:$data = <xml><user>root</user><pwd>ooxx</pwd></xml>]
 * @return [sting] $res  [响应数据]
 */
function httpPost($url,$data){
    //定义content-type为xml,注意是数组
    $header[] = "Content-type:text/xml"; 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 500);
    //防止出现验证错误
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $res = curl_exec($ch);
    if(curl_errno($ch)){
        print curl_error($ch);
    }
    curl_close($ch);

    return $res;
}


>>>函数名:httpGet

功能:向$url POST $data数据

code:

/**
 * [httpGet  get $url 返回的数据]
 * @param  [sting] $url  [目标url]
 * @return [sting] $res  [响应数据]
 */
function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
  }




>>fun:httpPost/httpGET>>功能:向$url POST/GET 数据

标签:curl类运用   采集函数   post数据   get数据   

原文地址:http://blog.csdn.net/amqvje/article/details/45417767

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!