Curl.class.php方法怎么在php中使用-創(chuàng)新互聯(lián)

本篇文章為大家展示了Curl.class.php方法怎么在php中使用,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供青田網站建設、青田做網站、青田網站設計、青田網站制作等企業(yè)網站建設、網頁設計與制作、青田企業(yè)網站模板建站服務,10余年青田做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。

具體如下:

<?php
//curl類
class Curl
{
 function Curl(){
  return true;
 }
 function execute($method, $url, $fields='', $userAgent='', $httpHeaders='', $username='', $password=''){
  $ch = Curl::create();
  if(false === $ch){
   return false;
  }
  if(is_string($url) && strlen($url)){
   $ret = curl_setopt($ch, CURLOPT_URL, $url);
  }else{
   return false;
  }
  //是否顯示頭部信息
  curl_setopt($ch, CURLOPT_HEADER, false);
  //
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  if($username != ''){
   curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  }
  $method = strtolower($method);
  if('post' == $method){
   curl_setopt($ch, CURLOPT_POST, true);
   if(is_array($fields)){
    $sets = array();
    foreach ($fields AS $key => $val){
     $sets[] = $key . '=' . urlencode($val);
    }
    $fields = implode('&',$sets);
   }
   curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  }else if('put' == $method){
   curl_setopt($ch, CURLOPT_PUT, true);
  }
  //curl_setopt($ch, CURLOPT_PROGRESS, true);
  //curl_setopt($ch, CURLOPT_VERBOSE, true);
  //curl_setopt($ch, CURLOPT_MUTE, false);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);//設置curl超時秒數(shù)
  if(strlen($userAgent)){
   curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  }
  if(is_array($httpHeaders)){
   curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
  }
  $ret = curl_exec($ch);
  if(curl_errno($ch)){
   curl_close($ch);
   return array(curl_error($ch), curl_errno($ch));
  }else{
   curl_close($ch);
   if(!is_string($ret) || !strlen($ret)){
    return false;
   }
   return $ret;
  }
 }
 function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
  $ret = Curl::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);
  if(false === $ret){
   return false;
  }
  if(is_array($ret)){
   return false;
  }
  return $ret;
 }
 function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
  $ret = Curl::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);
  if(false === $ret){
   return false;
  }
  if(is_array($ret)){
   return false;
  }
  return $ret;
 }
 function create(){
  $ch = null;
  if(!function_exists('curl_init')){
   return false;
  }
  $ch = curl_init();
  if(!is_resource($ch)){
   return false;
  }
  return $ch;
 }
}
?>

GET用法:

$curl = new Curl();
$curl->get('http://www.XXX.com/');

POST用法:

$curl = new Curl();
$curl->get('http://www.XXX.com/', 'p=1&time=0');

上述內容就是Curl.class.php方法怎么在php中使用,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章名稱:Curl.class.php方法怎么在php中使用-創(chuàng)新互聯(lián)
網站路徑:http://muchs.cn/article42/deochc.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供外貿建站、ChatGPT關鍵詞優(yōu)化、網站策劃網站維護、全網營銷推廣

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司