ใน PHP ฉันเห็นคำว่า cURL ในหลายโครงการ PHP มันคืออะไร? มันทำงานยังไง?
ลิงค์อ้างอิง: cURL
ใน PHP ฉันเห็นคำว่า cURL ในหลายโครงการ PHP มันคืออะไร? มันทำงานยังไง?
ลิงค์อ้างอิง: cURL
คำตอบ:
cURLเป็นห้องสมุดที่ให้คุณสร้างคำขอ HTTP ใน PHP ทุกสิ่งที่คุณจำเป็นต้องรู้เกี่ยวกับมัน (และนามสกุลอื่น ๆ ส่วนใหญ่) สามารถพบได้ในคู่มือ PHP
ในการใช้ฟังก์ชัน cURL ของ PHP คุณต้องติดตั้ง» libcurl package PHP ต้องการให้คุณใช้ libcurl 7.0.2-beta หรือสูงกว่า ใน PHP 4.2.3 คุณจะต้องใช้ libcurl เวอร์ชั่น 7.9.0 หรือสูงกว่า จาก PHP 4.3.0 คุณจะต้องใช้เวอร์ชัน libcurl ที่ 7.9.8 หรือสูงกว่า PHP 5.0.0 ต้องใช้ libcurl เวอร์ชั่น 7.10.5 หรือสูงกว่า
คุณสามารถส่งคำขอ HTTP โดยไม่มี cURL ได้เช่นกันแม้ว่าจะต้องallow_url_fopenเปิดใช้งานในphp.iniไฟล์ของคุณก็ตาม
// Make a HTTP GET request and print it (requires allow_url_fopen to be enabled)
print file_get_contents('http://www.example.com/');
              cURL เป็นวิธีที่คุณสามารถกด URL จากรหัสของคุณเพื่อรับการตอบสนอง html จากมัน cURL หมายถึง URL ไคลเอนต์ที่อนุญาตให้คุณเชื่อมต่อกับ URL อื่นและใช้การตอบกลับของพวกเขาในรหัสของคุณ
สรุป:
curl_execคำสั่งใน PHP เป็นสะพานที่จะใช้curlจากคอนโซล curl_exec ทำให้การร้องขอ GET / POST ง่ายและรวดเร็วรับการตอบกลับจากเซิร์ฟเวอร์อื่นเช่น JSON และดาวน์โหลดไฟล์
คำเตือน, อันตราย:
curlเป็นสิ่งที่ชั่วร้ายและอันตรายหากใช้อย่างไม่ถูกต้องเพราะเป็นข้อมูลเกี่ยวกับการรับข้อมูลจากอินเทอร์เน็ต บางคนอาจได้รับระหว่าง curl ของคุณและเซิร์ฟเวอร์อื่นและฉีดrm -rf /เข้าไปในคำตอบของคุณแล้วทำไมฉันถึงไปที่คอนโซลและls -lไม่ทำงานอีกต่อไป? เพราะคุณประมาทพลังอันตรายของการขด อย่าไว้ใจสิ่งใดที่กลับมาจากการม้วนเพื่อความปลอดภัยแม้ว่าคุณจะพูดคุยกับเซิร์ฟเวอร์ของคุณเอง คุณสามารถดึงมัลแวร์กลับมาเพื่อบรรเทาความมั่งคั่งของคนโง่  
สิ่งเหล่านี้ทำบน Ubuntu 12.10
Curl พื้นฐานจาก commandline:
el@apollo:/home/el$ curl http://i.imgur.com/4rBHtSm.gif > mycat.gif
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  492k  100  492k    0     0  1077k      0 --:--:-- --:--:-- --:--:-- 1240k
จากนั้นคุณสามารถเปิด gif ของคุณใน firefox:
firefox mycat.gif
แมวที่มีชื่อเสียงกำลังพัฒนา Toxoplasma gondii เพื่อให้ผู้หญิงอยู่รอบ ๆ แมวและผู้ชายก็เช่นกันที่จะทำให้ผู้หญิงอยู่ใกล้
ตัวอย่าง cURL รับคำขอเพื่อเข้าสู่ google.com, echo to commandline:
สิ่งนี้ทำผ่านเทอร์มินัล phpsh:
php> $ch = curl_init();
php> curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');
php> curl_exec($ch);
ซึ่งพิมพ์และทิ้งระเบียบ HTML และ javascript แบบย่อ (จาก google) ไปยังคอนโซล
ตัวอย่าง cURL นำข้อความตอบกลับไปไว้ในตัวแปร:
สิ่งนี้ทำผ่านเทอร์มินัล phpsh:
php> $ch = curl_init();
php> curl_setopt($ch, CURLOPT_URL, 'http://i.imgur.com/wtQ6yZR.gif');
php> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
php> $contents = curl_exec($ch);
php> echo $contents;
ตอนนี้ตัวแปรมีไบนารีซึ่งเป็น gif ที่เคลื่อนไหวได้ของแมวความเป็นไปได้ไม่มีที่สิ้นสุด
ทำ curl จากภายในไฟล์ PHP:
ใส่รหัสนี้ในไฟล์ชื่อ myphp.php:
<?php
  $curl_handle=curl_init();
  curl_setopt($curl_handle,CURLOPT_URL,'http://www.google.com');
  curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
  curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  $buffer = curl_exec($curl_handle);
  curl_close($curl_handle);
  if (empty($buffer)){
      print "Nothing returned from url.<p>";
  }
  else{
      print $buffer;
  }
?>
จากนั้นรันผ่าน commandline:
php < myphp.php
คุณรัน myphp.php และดำเนินการคำสั่งเหล่านั้นผ่านทางล่าม php และทิ้ง HTML และ javascript ที่ยุ่งเหยิงไปที่หน้าจอ
คุณสามารถทำได้GETและPOSTร้องขอด้วย curl ทั้งหมดที่คุณทำได้คือระบุพารามิเตอร์ตามที่กำหนดไว้ที่นี่: การใช้ curl เพื่อทำให้งาน HTTP เป็นแบบอัตโนมัติ
คำเตือนถึงอันตราย:
ระวังการโยนทิ้งอย่างระมัดระวังถ้ามีการตีความและดำเนินการใด ๆ กล่องของคุณจะเป็นเจ้าของและข้อมูลบัตรเครดิตของคุณจะถูกขายให้กับบุคคลที่สาม ด้านหน้าสำหรับแหวนอาชญากรรมการฉ้อโกงบัตรเครดิตต่างประเทศ
cURL เป็นวิธีที่คุณสามารถกด URL จากรหัสของคุณเพื่อรับการตอบกลับ HTML จากมัน มันใช้สำหรับคำสั่ง cURL จากภาษา PHP
<?php
// Step 1
$cSession = curl_init(); 
// Step 2
curl_setopt($cSession,CURLOPT_URL,"http://www.google.com/search?q=curl");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false); 
// Step 3
$result=curl_exec($cSession);
// Step 4
curl_close($cSession);
// Step 5
echo $result;
?> 
ขั้นตอนที่ 1: curl_init()เริ่มต้นเซสชั่นโดยใช้ขด
ขั้นตอนที่ 2: CURLOPT_URLการตั้งค่าตัวเลือกสำหรับการ ค่านี้เป็น URL ที่เราจะส่งคำขอไป ผนวกคำค้นหาโดยใช้พารามิเตอร์curl ตัวเลือกชุดสำหรับq= CURLOPT_RETURNTRANSFERTrue จะบอกให้ curl ส่งคืนสตริงแทนที่จะพิมพ์ออกมา ตั้งค่าตัวเลือกสำหรับCURLOPT_HEADERเท็จจะบอกให้ขดไม่สนใจส่วนหัวในค่าส่งคืน
ขั้นตอนที่ 3: Execute curl_exec()เซสชั่นโดยใช้ขด
ขั้นตอนที่ 4: ปิดเซสชัน curl ที่เราสร้าง
ขั้นตอนที่ 5: ส่งออกสตริงส่งคืน
public function curlCall($apiurl, $auth, $rflag)
{
    $ch = curl_init($apiurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if($auth == 'auth') { 
        curl_setopt($ch, CURLOPT_USERPWD, "passw:passw");
    } else {
        curl_setopt($ch, CURLOPT_USERPWD, "ss:ss1");
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $dt = curl_exec($ch);        
    curl_close($ch);
    if($rflag != 1) {
        $dt = json_decode($dt,true);        
    }
    return $dt;
}
นอกจากนี้ยังใช้สำหรับการรับรองความถูกต้อง นอกจากนี้เรายังสามารถตั้งชื่อผู้ใช้และรหัสผ่านสำหรับการตรวจสอบ
สำหรับฟังก์ชันเพิ่มเติมดูคู่มือผู้ใช้หรือบทช่วยสอนต่อไปนี้:
http://php.net/manual/en/ref.curl.php 
http://www.startutorial.com/articles/view/php-curl
ก่อนอื่นให้เราเข้าใจแนวคิดของ curl, libcurl และ PHP / cURL
curl : เครื่องมือบรรทัดคำสั่งสำหรับรับหรือส่งไฟล์โดยใช้ไวยากรณ์ URL
libcurl : ไลบรารีที่สร้างโดย Daniel Stenberg ที่ให้คุณเชื่อมต่อและสื่อสารกับเซิร์ฟเวอร์หลายชนิดด้วยโปรโตคอลประเภทต่าง ๆ ปัจจุบัน libcurl รองรับโปรโตคอล http, https, ftp, gopher, telnet, dict, file และ ldap libcurl ยังรองรับใบรับรอง HTTPS, HTTP POST, HTTP PUT, การอัพโหลด FTP (สามารถทำได้ด้วยการขยาย ftp ของ PHP), การอัพโหลดตามรูปแบบ HTTP, พร็อกซี่, คุกกี้, และการตรวจสอบผู้ใช้ + รหัสผ่าน
PHP / cURL : โมดูลสำหรับ PHP ที่ทำให้โปรแกรม PHP ใช้ libcurl ได้
วิธีใช้:
ขั้นที่ 1 : เริ่มต้นเซสชัน curl ใช้ curl_init ()
ขั้นที่ 2 : ตั้งค่าตัวเลือกสำหรับ CURLOPT_URL ค่านี้คือ URL ที่เรากำลังส่งคำขอไปยังผนวกคำค้นหา "curl" โดยใช้พารามิเตอร์ "q =" ตั้งค่าตัวเลือก CURLOPT_RETURNTRANSFER จริงจะบอก curl ให้คืนสตริงแทนที่จะพิมพ์ออกมา ตั้งค่าตัวเลือกสำหรับ CURLOPT_HEADER false จะบอกให้ curl ละเว้นส่วนหัวในค่าส่งคืน
ขั้นที่ 3 : ดำเนินการเซสชัน curl โดยใช้ curl_exec ()
ขั้นที่ 4 : ปิดเซสชัน curl ที่เราสร้าง
ขั้นที่ 5 : ส่งออกสตริงส่งคืน
ทำ DEMO :
คุณจะต้องสร้างไฟล์ PHP สองไฟล์และวางไว้ในโฟลเดอร์ที่เว็บเซิร์ฟเวอร์ของคุณสามารถให้บริการไฟล์ PHP ได้ ในกรณีของฉันฉันใส่ลงใน / var / www / เพื่อความเรียบง่าย
1. helloservice.phpและ2. demo.php
helloservice.php นั้นง่ายมากและเพียงแค่สะท้อนข้อมูลกลับที่ได้รับ:
<?php
  // Here is the data we will be sending to the service
  $some_data = array(
    'message' => 'Hello World', 
    'name' => 'Anand'
  );  
  $curl = curl_init();
  // You can also set the URL you want to communicate with by doing this:
  // $curl = curl_init('http://localhost/echoservice');
  // We POST the data
  curl_setopt($curl, CURLOPT_POST, 1);
  // Set the url path we want to call
  curl_setopt($curl, CURLOPT_URL, 'http://localhost/demo.php');  
  // Make it so the data coming back is put into a string
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  // Insert the data
  curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data);
  // You can also bunch the above commands into an array if you choose using: curl_setopt_array
  // Send the request
  $result = curl_exec($curl);
  // Get some cURL session information back
  $info = curl_getinfo($curl);  
  echo 'content type: ' . $info['content_type'] . '<br />';
  echo 'http code: ' . $info['http_code'] . '<br />';
  // Free up the resources $curl is using
  curl_close($curl);
  echo $result;
?>
2.demo.phpหน้าคุณสามารถดูผลลัพธ์:
<?php 
   print_r($_POST);
   //content type: text/html; charset=UTF-8
   //http code: 200
   //Array ( [message] => Hello World [name] => Anand )
?>
              ส่วนขยาย cURL ไปยัง PHP ได้รับการออกแบบมาเพื่อให้คุณสามารถใช้ทรัพยากรเว็บที่หลากหลายจากภายในสคริปต์ PHP ของคุณ
cURL ใน PHP เป็นบริดจ์ในการใช้บรรทัดคำสั่ง cURL จากภาษา PHP
PHP รองรับ libcurl ซึ่งเป็นไลบรารีที่สร้างโดย Daniel Stenberg ที่ให้คุณเชื่อมต่อและสื่อสารกับเซิร์ฟเวอร์หลายประเภทด้วยโปรโตคอลประเภทต่าง ๆ ปัจจุบัน libcurl รองรับโปรโตคอล http, https, ftp, gopher, telnet, dict, file และ ldap libcurl ยังรองรับใบรับรอง HTTPS, HTTP POST, HTTP PUT, การอัพโหลด FTP (สามารถทำได้ด้วยการขยาย ftp ของ PHP), การอัพโหลดตามรูปแบบ HTTP, พร็อกซี่, คุกกี้, และการตรวจสอบผู้ใช้ + รหัสผ่าน
เมื่อคุณรวบรวม PHP ด้วยการสนับสนุน cURL คุณสามารถเริ่มใช้ฟังก์ชัน cURL ได้ แนวคิดพื้นฐานที่อยู่เบื้องหลังฟังก์ชัน cURL คือคุณเริ่มต้นเซสชัน cURL โดยใช้ curl_init () จากนั้นคุณสามารถตั้งค่าตัวเลือกทั้งหมดสำหรับการถ่ายโอนผ่าน curl_setopt () จากนั้นคุณสามารถดำเนินการเซสชันด้วย curl_exec () และจากนั้นคุณ เสร็จสิ้นเซสชั่นของคุณโดยใช้ curl_close ()
// error reporting
error_reporting(E_ALL);
ini_set("display_errors", 1);
//setting url
$url = 'http://example.com/api';
//data
$data = array("message" => "Hello World!!!");
try {
    $ch = curl_init($url);
    $data_string = json_encode($data);
    if (FALSE === $ch)
        throw new Exception('failed to initialize');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        $output = curl_exec($ch);
    if (FALSE === $output)
        throw new Exception(curl_error($ch), curl_errno($ch));
    // ...process $output now
} catch(Exception $e) {
    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
}
Curl ไม่มีอะไรนอกจากส่วนขยายของ PHP ซึ่งสืบทอดพฤติกรรมของคำสั่ง curl & ไลบรารีที่เขียนเป็นหลักสำหรับเครื่องมือบรรทัดคำสั่ง Linux / Unix
Curl คืออะไร cURL ย่อมาจาก URL ลูกค้า cURL ใช้เพื่อส่งข้อมูลไปยัง URL ใด ๆ สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับสิ่งที่เป็นขดคุณสามารถเยี่ยมชมเว็บไซต์ CURL
cURL ใน PHP ตอนนี้มีการนำเสนอแนวคิดเดียวกันใน PHP เพื่อส่งข้อมูลไปยัง URL ที่สามารถเข้าถึงได้ผ่านโปรโตคอลอื่นเช่น HTTP หรือ FTP สำหรับรายละเอียดเพิ่มเติมคุณอาจอ้างอิงกับPHP Curl Tutorial
ฟังก์ชั่น php curl (POST, GET, DELETE, PUT)
function curl($post = array(), $url, $token = '', $method = "POST", $json = false, $ssl = true){
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url);    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    if($method == 'POST'){
        curl_setopt($ch, CURLOPT_POST, 1);
    }
    if($json == true){
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json','Authorization: Bearer '.$token,'Content-Length: ' . strlen($post)));
    }else{
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSLVERSION, 6);
    if($ssl == false){
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    }
    // curl_setopt($ch, CURLOPT_HEADER, 0);     
    $r = curl_exec($ch);    
    if (curl_error($ch)) {
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $err = curl_error($ch);
        print_r('Error: ' . $err . ' Status: ' . $statusCode);
        // Add error
        $this->error = $err;
    }
    curl_close($ch);
    return $r;
}
              php curl class (GET, POST, อัพโหลดไฟล์, เซสชั่น, ส่ง JSON POST, บังคับใช้ SSL / TLS ด้วยตนเอง):
<?php
    // Php curl class
    class Curl {
        public $error;
        function __construct() {}
        function Get($url = "http://hostname.x/api.php?q=jabadoo&txt=gin", $forceSsl = false,$cookie = "", $session = true){
            // $url = $url . "?". http_build_query($data);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, false);        
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            if($session){
                curl_setopt($ch, CURLOPT_COOKIESESSION, true );
                curl_setopt($ch , CURLOPT_COOKIEJAR, 'cookies.txt');
                curl_setopt($ch , CURLOPT_COOKIEFILE, 'cookies.txt');
            }
            if($forceSsl){
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 1, 2
            }
            if(!empty($cookie)){            
                curl_setopt($ch, CURLOPT_COOKIE, $cookie); // "token=12345"
            }
            $info = curl_getinfo($ch);
            $res = curl_exec($ch);        
            if (curl_error($ch)) {
                $this->error = curl_error($ch);
                throw new Exception($this->error);
            }else{
                curl_close($ch);
                return $res;
            }        
        }
        function GetArray($url = "http://hostname.x/api.php", $data = array("name" => "Max", "age" => "36"), $forceSsl = false, $cookie = "", $session = true){
            $url = $url . "?". http_build_query($data);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            if($session){
                curl_setopt($ch, CURLOPT_COOKIESESSION, true );
                curl_setopt($ch , CURLOPT_COOKIEJAR, 'cookies.txt');
                curl_setopt($ch , CURLOPT_COOKIEFILE, 'cookies.txt');
            }
            if($forceSsl){
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 1, 2
            }
            if(!empty($cookie)){
                curl_setopt($ch, CURLOPT_COOKIE, $cookie); // "token=12345"
            }
            $info = curl_getinfo($ch);
            $res = curl_exec($ch);        
            if (curl_error($ch)) {
                $this->error = curl_error($ch);
                throw new Exception($this->error);
            }else{
                curl_close($ch);
                return $res;
            }        
        }
        function PostJson($url = "http://hostname.x/api.php", $data = array("name" => "Max", "age" => "36"), $forceSsl = false, $cookie = "", $session = true){
            $data = json_encode($data);
            $ch = curl_init($url);                                                                      
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            if($session){
                curl_setopt($ch, CURLOPT_COOKIESESSION, true );
                curl_setopt($ch , CURLOPT_COOKIEJAR, 'cookies.txt');
                curl_setopt($ch , CURLOPT_COOKIEFILE, 'cookies.txt');
            }
            if($forceSsl){
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 1, 2
            }
            if(!empty($cookie)){
                curl_setopt($ch, CURLOPT_COOKIE, $cookie); // "token=12345"
            }
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Authorization: Bearer helo29dasd8asd6asnav7ffa',                                                      
                'Content-Type: application/json',                                                                                
                'Content-Length: ' . strlen($data))                                                                       
            );        
            $res = curl_exec($ch);
            if (curl_error($ch)) {
                $this->error = curl_error($ch);
                throw new Exception($this->error);
            }else{
                curl_close($ch);
                return $res;
            } 
        }
        function Post($url = "http://hostname.x/api.php", $data = array("name" => "Max", "age" => "36"), $files = array('ads/ads0.jpg', 'ads/ads1.jpg'), $forceSsl = false, $cookie = "", $session = true){
            foreach ($files as $k => $v) {
                $f = realpath($v);
                if(file_exists($f)){
                    $fc = new CurlFile($f, mime_content_type($f), basename($f)); 
                    $data["file[".$k."]"] = $fc;
                }
            }
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");        
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
            curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // !!!! required as of PHP 5.6.0 for files !!!
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            if($session){
                curl_setopt($ch, CURLOPT_COOKIESESSION, true );
                curl_setopt($ch , CURLOPT_COOKIEJAR, 'cookies.txt');
                curl_setopt($ch , CURLOPT_COOKIEFILE, 'cookies.txt');
            }
            if($forceSsl){
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 1, 2
            }
            if(!empty($cookie)){
                curl_setopt($ch, CURLOPT_COOKIE, $cookie); // "token=12345"
            }
            $res = curl_exec($ch);
            if (curl_error($ch)) {
                $this->error = curl_error($ch);
                throw new Exception($this->error);
            }else{
                curl_close($ch);
                return $res;
            } 
        }
    }
?>
ตัวอย่าง:
<?php
    $urlget = "http://hostname.x/api.php?id=123&user=bax";
    $url = "http://hostname.x/api.php";
    $data = array("name" => "Max", "age" => "36");
    $files = array('ads/ads0.jpg', 'ads/ads1.jpg');
    $curl = new Curl();
    echo $curl->Get($urlget, true, "token=12345");
    echo $curl->GetArray($url, $data, true);
    echo $curl->Post($url, $data, $files, true);
    echo $curl->PostJson($url, $data, true);
?>
ไฟล์ php: api.php
<?php
    /*
    $Cookie = session_get_cookie_params();
    print_r($Cookie);
    */
    session_set_cookie_params(9000, '/', 'hostname.x', isset($_SERVER["HTTPS"]), true);
    session_start();
    $_SESSION['cnt']++;
    echo "Session count: " . $_SESSION['cnt']. "\r\n";
    echo $json = file_get_contents('php://input');
    $arr = json_decode($json, true);
    echo "<pre>";
    if(!empty($json)){ print_r($arr); }
    if(!empty($_GET)){ print_r($_GET); }
    if(!empty($_POST)){ print_r($_POST); }
    if(!empty($_FILES)){ print_r($_FILES); }
    // request headers
    print_r(getallheaders());
    print_r(apache_response_headers());
    // Fetch a list of headers to be sent.
    // print_r(headers_list());
?>