Ví dụ cuộn tròn php 8

1. Từng bước một

  • Khởi tạo phiên cURL
$url = "www.domain.com";
$ch = curl_init($url);
  • Nếu yêu cầu của bạn có các tiêu đề như mã thông báo mang hoặc xác định nội dung JSON, bạn phải đặt tùy chọn
    $token = "generated token code";
    curl_setopt(
        $ch, 
        CURLOPT_HTTPHEADER, 
        array(
            'Content-Type: application/json', // for define content type that is json
            'bearer: '.$token, // send token in header request
            'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
        )
    );
    
    2 thành cURL
$token = "generated token code";
curl_setopt(
    $ch, 
    CURLOPT_HTTPHEADER, 
    array(
        'Content-Type: application/json', // for define content type that is json
        'bearer: '.$token, // send token in header request
        'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
    )
);
  • Nếu bạn muốn bao gồm tiêu đề trong đầu ra, hãy đặt
    $token = "generated token code";
    curl_setopt(
        $ch, 
        CURLOPT_HTTPHEADER, 
        array(
            'Content-Type: application/json', // for define content type that is json
            'bearer: '.$token, // send token in header request
            'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
        )
    );
    
    3 thành
    $token = "generated token code";
    curl_setopt(
        $ch, 
        CURLOPT_HTTPHEADER, 
        array(
            'Content-Type: application/json', // for define content type that is json
            'bearer: '.$token, // send token in header request
            'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
        )
    );
    
    4
curl_setopt($ch, CURLOPT_HEADER, false);
  • Đặt tùy chọn
    $token = "generated token code";
    curl_setopt(
        $ch, 
        CURLOPT_HTTPHEADER, 
        array(
            'Content-Type: application/json', // for define content type that is json
            'bearer: '.$token, // send token in header request
            'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
        )
    );
    
    5 thành
    $token = "generated token code";
    curl_setopt(
        $ch, 
        CURLOPT_HTTPHEADER, 
        array(
            'Content-Type: application/json', // for define content type that is json
            'bearer: '.$token, // send token in header request
            'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
        )
    );
    
    4 để trả lại chuyển khoản dưới dạng chuỗi thay vì xuất trực tiếp
________số 8_______
  • Để kiểm tra sự tồn tại của một tên phổ biến trong chứng chỉ ngang hàng SSL có thể được đặt thành
    $token = "generated token code";
    curl_setopt(
        $ch, 
        CURLOPT_HTTPHEADER, 
        array(
            'Content-Type: application/json', // for define content type that is json
            'bearer: '.$token, // send token in header request
            'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
        )
    );
    
    7,
    $token = "generated token code";
    curl_setopt(
        $ch, 
        CURLOPT_HTTPHEADER, 
        array(
            'Content-Type: application/json', // for define content type that is json
            'bearer: '.$token, // send token in header request
            'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
        )
    );
    
    8,
    $token = "generated token code";
    curl_setopt(
        $ch, 
        CURLOPT_HTTPHEADER, 
        array(
            'Content-Type: application/json', // for define content type that is json
            'bearer: '.$token, // send token in header request
            'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
        )
    );
    
    9
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  • Để đăng các trường dưới dạng một mảng bằng cURL
$fields = array(
    "username" => "user1",
    "password" => "passuser1",
    "gender" => 1
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  • Thực thi cURL và trả về chuỗi. tùy thuộc vào tài nguyên của bạn, điều này trả về đầu ra như
    curl_setopt($ch, CURLOPT_HEADER, false);
    
    0
$result = curl_exec($ch);
  • Đóng tài nguyên cURL và giải phóng tài nguyên hệ thống
curl_close($ch);

2. Sử dụng như một lớp học

  • Toàn bộ lớp học
    curl_setopt($ch, CURLOPT_HEADER, false);
    
    1 có thể được mở rộng
class class_name_for_call_cURL {
    protected function getUrl() {
        return "www.domain.com";
    }

    public function call_cURL() {
        $token = "generated token code";

        $fields = array(
            "username" => "user1",
            "password" => "passuser1",
            "gender" => 1
        );

        $url = $this->getUrl();
        $output = $this->_execute($fields, $url, $token);
        
        // if you want to get json data
        // $output = json_decode($output);
            
        if ($output == "OK") {
            return true;
        } else {
             return false;
        }
    }

    private function _execute($postData, $url, $token) {
        // for sending data as json type
        $fields = json_encode($postData);

        $ch = curl_init($url);
        curl_setopt(
            $ch, 
            CURLOPT_HTTPHEADER, 
            array(
                'Content-Type: application/json', // if the content type is json
                'bearer: '.$token // if you need token in header
            )
        );
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }
}
  • Sử dụng lớp và gọi cURL
$class = new class_name_for_call_cURL();
var_dump($class->call_cURL()); // output is true/false

3. một chức năng

  • Một chức năng để sử dụng bất cứ nơi nào cần thiết
$token = "generated token code";
curl_setopt(
    $ch, 
    CURLOPT_HTTPHEADER, 
    array(
        'Content-Type: application/json', // for define content type that is json
        'bearer: '.$token, // send token in header request
        'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
    )
);
0
  • Chức năng này có thể sử dụng được chỉ bằng cách
$token = "generated token code";
curl_setopt(
    $ch, 
    CURLOPT_HTTPHEADER, 
    array(
        'Content-Type: application/json', // for define content type that is json
        'bearer: '.$token, // send token in header request
        'Content-length: 100' // content length for example 100 characters (can add by strlen($fields))
    )
);
1