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

PHP CURL

时间:2016-10-27 12:50:15      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:public   code   array   https   content   执行   header   stat   设置   

public function http_get($url){
  $ch = curl_init();
  //设置超时
  curl_setopt($ch, CURLOP_TIMEOUT, 5);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $res = curl_exec($ch);
  curl_close($ch);
  return $res;
}

public function http_post($url,$param){
  $oCurl = curl_init();
  if(stripos($url,"https://")!==FALSE){
    curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  }
  curl_setopt($oCurl, CURLOPT_URL, $url);
  curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true );
  curl_setopt($oCurl, CURLOPT_TIMEOUT, 5);//允许执行的最长秒数
  curl_setopt($oCurl, CURLOPT_POST,true);
  curl_setopt($oCurl, CURLOPT_POSTFIELDS,$param);
  curl_setopt($oCurl, CURLOPT_HTTPHEADER, array(
  ‘Content-Type: application/json‘,
  ‘Content-Length: ‘ . strlen($param))
  );
  $sContent = curl_exec($oCurl);
  $aStatus = curl_getinfo($oCurl);
  curl_close($oCurl);
  if(intval($aStatus["http_code"])==200){
    return $sContent;
  }else{
    return false;
  }
}

 

PHP CURL

标签:public   code   array   https   content   执行   header   stat   设置   

原文地址:http://www.cnblogs.com/ding-daisy/p/6003310.html

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