Hướng dẫn php curl vs file_get_contents - php curl so với file_get_contents

Tôi biết đó là một chủ đề cũ nhưng tôi tin rằng điều này thực sự quan trọng. Và bây giờ, có rất nhiều sự khác biệt hơn 8 năm trước. Như chúng ta đã biết, Curl là thư viện phần thứ 3.

So sánh đơn giản: Phiên bản cuối cùng của Thư viện Curl có hơn 170 chức năng khác nhau để có thể gửi yêu cầu thích hợp đến API. Chỉ có 70 chức năng 8 năm trước. Sự thật: Vẫn đang được phát triển.: Last version of Curl library has more than 170 different functions to be able to send proper request to APIs. There ware only 70 functions 8 years ago. Fact: still under development.

Đó là lý do tại sao tôi muốn đưa ra một nhận xét mới cho câu hỏi này.

File_Get_Contents () là gì

File_Get_Contents () là một hàm hệ thống tập tin trong PHP mà bạn có thể đọc nội dung từ một tệp và thực hiện các yêu cầu bằng các phương thức GET và POST. Bạn có thể thêm tham số vào yêu cầu của mình trong khi bạn đang sử dụng hàm file_get_contents (). Bạn có thể thấy mẫu dưới đây.

$data = http_build_query(
    array(
        'user_id'   => '558673',
        'user_name' => 'John Doe'
    )
);

$config = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $data
    )
);

$context = stream_context_create($config);

$result = file_get_contents('https://google.com', false, $context);

Curl () là gì

Curl là thư viện bên thứ ba nguồn mở. Bạn có thể đến được repo git từ đây. Hàm này "mô phỏng" các yêu cầu và phản hồi HTTP. Mô phỏng này cho phép bạn xử lý các yêu cầu HTTP Async và chuyển dữ liệu phức tạp. Ngoài ra, Curl phù hợp để thực hiện yêu cầu FTP dựa trên miền chéo. Nó có thể được sử dụng trong các ứng dụng khác nhau như thu thập dữ liệu từ một trang web và thiết lập proxy.simulates" HTTP requests and responses. This simulation allows you handling async HTTP requests and complex data transfers. In addition, Curl is suitable for performing cross-domain based FTP request. It can be used in various apps like data crawling from a website and proxy setup.

Hãy kiểm tra cú pháp yêu cầu Curl.

$url = API_ENDPOINT."/get_movies";
        
  $curl = curl_init();
         
  $params = array(
    'category' => $category,
    'limit' => $limit,
    'start' => $start,
    'order' => $order,
    'term' => $term
  );

  $params_string = http_build_query($params);

  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, TRUE);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $params_string);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  
  $data = curl_exec($curl);     
  curl_close($curl);

  echo json_decode($data,TRUE); //service returns json in this sample

Lưu ý: Đây là mẫu cơ bản của yêu cầu Curl. Bạn có thể thêm nhiều tham số và tùy chọn vào đối tượng Curl bằng các hàm của nó như Curlopt_httpheader, Curlopt_SSL_Verifypeer. Những loại tham số này tùy thuộc vào bạn và dịch vụ mà bạn đang cố gắng sử dụng. This is the basic sample of a curl request. You can add more parameter and options to the curl object using its functions like CURLOPT_HTTPHEADER, CURLOPT_SSL_VERIFYPEER. These kind of parameters all up to you and the service that you trying to use.

Curl vs file_get_contents ()

  • CURL có thể xử lý các giao tiếp HTML phức tạp, nhưng file_get_contents() thì không.
  • CURL hỗ trợ HTTP PUT, GET, POST nhưng file_get_contents() hỗ trợ các yêu cầu bài đăng HTTP và HTTP đơn giản.
  • CURL hỗ trợ bộ nhớ đệm và cookie nhưng file_get_contents() không hỗ trợ bộ đệm, cookie, v.v.
  • CURL có thể sử dụng HTTP, HTTPS, FTP, FTP và nhiều hơn nữa. file_get_contents() sử dụng các giao thức HTTP và HTTPS để liên lạc.
  • CURL có thể được sử dụng để đọc, cập nhật và xóa các tệp khỏi máy chủ, nhưng file_get_contents() chỉ cho phép bạn đọc một tệp.
  • CURL an toàn và nhanh hơn file_get_contents()
  • CURL phức tạp hơn một chút để hiểu hơn file_get_contents().

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc Function: This PHP function is used to retrieve the contents of a file. The contents can be stored as a string variable. Alternatively, it also simulates HTTP transactions, involving requests via GET method and responses using POST method respectively. Primarily, it is best-suited for simple HTTP manipulations and to get single-line JSON responses. 

    Example:

    PHP

    Đầu ra: Nó sẽ chuyển hướng đến trang chủ Geekforgeek.It will redirect to GeeksforGeeks home page.

    Phương thức file_get_contents ()It is a third party library that simulates HTTP requests and responses in a much more efficient way. It can handle asynchronous HTTP requests and complex communications like callback functions or break point continual transferring. It is also suitable for carrying out cross-domain based FTP request. Also, it can be used in different applications like proxy setup and Website Scraping etc. 

    Example:

    PHP

    $url = API_ENDPOINT."/get_movies";
            
      $curl = curl_init();
             
      $params = array(
        'category' => $category,
        'limit' => $limit,
        'start' => $start,
        'order' => $order,
        'term' => $term
      );
    
      $params_string = http_build_query($params);
    
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_POST, TRUE);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $params_string);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
      
      $data = curl_exec($curl);     
      curl_close($curl);
    
      echo json_decode($data,TRUE); //service returns json in this sample
    
    6

    $url = API_ENDPOINT."/get_movies";
            
      $curl = curl_init();
             
      $params = array(
        'category' => $category,
        'limit' => $limit,
        'start' => $start,
        'order' => $order,
        'term' => $term
      );
    
      $params_string = http_build_query($params);
    
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_POST, TRUE);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $params_string);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
      
      $data = curl_exec($curl);     
      curl_close($curl);
    
      echo json_decode($data,TRUE); //service returns json in this sample
    
    7
    $url = API_ENDPOINT."/get_movies";
            
      $curl = curl_init();
             
      $params = array(
        'category' => $category,
        'limit' => $limit,
        'start' => $start,
        'order' => $order,
        'term' => $term
      );
    
      $params_string = http_build_query($params);
    
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_POST, TRUE);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $params_string);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
      
      $data = curl_exec($curl);     
      curl_close($curl);
    
      echo json_decode($data,TRUE); //service returns json in this sample
    
    8

    $url = API_ENDPOINT."/get_movies";
            
      $curl = curl_init();
             
      $params = array(
        'category' => $category,
        'limit' => $limit,
        'start' => $start,
        'order' => $order,
        'term' => $term
      );
    
      $params_string = http_build_query($params);
    
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_POST, TRUE);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $params_string);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
      
      $data = curl_exec($curl);     
      curl_close($curl);
    
      echo json_decode($data,TRUE); //service returns json in this sample
    
    9
    $url = API_ENDPOINT."/get_movies";
            
      $curl = curl_init();
             
      $params = array(
        'category' => $category,
        'limit' => $limit,
        'start' => $start,
        'order' => $order,
        'term' => $term
      );
    
      $params_string = http_build_query($params);
    
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_POST, TRUE);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $params_string);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
      
      $data = curl_exec($curl);     
      curl_close($curl);
    
      echo json_decode($data,TRUE); //service returns json in this sample
    
    7CURL1

    $url = API_ENDPOINT."/get_movies";
            
      $curl = curl_init();
             
      $params = array(
        'category' => $category,
        'limit' => $limit,
        'start' => $start,
        'order' => $order,
        'term' => $term
      );
    
      $params_string = http_build_query($params);
    
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_POST, TRUE);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $params_string);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
      
      $data = curl_exec($curl);     
      curl_close($curl);
    
      echo json_decode($data,TRUE); //service returns json in this sample
    
    9
    $url = API_ENDPOINT."/get_movies";
            
      $curl = curl_init();
             
      $params = array(
        'category' => $category,
        'limit' => $limit,
        'start' => $start,
        'order' => $order,
        'term' => $term
      );
    
      $params_string = http_build_query($params);
    
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_POST, TRUE);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $params_string);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
      
      $data = curl_exec($curl);     
      curl_close($curl);
    
      echo json_decode($data,TRUE); //service returns json in this sample
    
    7CURL4CURL5CURL6

    CURL7 CURL8

    $url = API_ENDPOINT."/get_movies";
            
      $curl = curl_init();
             
      $params = array(
        'category' => $category,
        'limit' => $limit,
        'start' => $start,
        'order' => $order,
        'term' => $term
      );
    
      $params_string = http_build_query($params);
    
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_POST, TRUE);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $params_string);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
      
      $data = curl_exec($curl);     
      curl_close($curl);
    
      echo json_decode($data,TRUE); //service returns json in this sample
    
    7CURL6

    file_get_contents()1 CURL7file_get_contents()3

    file_get_contents()4

    Đầu ra: Nó sẽ chuyển hướng đến trang chủ Geekforgeek.It will redirect to GeeksforGeeks home page.

    Phương thức file_get_contents ()Xoăn
    Xử lý truyền thông HTTP đơn giản.Xử lý truyền thông HTTP phức tạp.
    Hỗ trợ các hoạt động bài HTTP HTTP đơn giản và HTTP.Hỗ trợ HTTP, chứng chỉ ngoài việc nhận và đăng yêu cầu.
    Không hỗ trợ bộ đệm, cookie, v.v.Hỗ trợ bộ nhớ đệm, báo cáo tiến độ cookie, v.v.
    Nó sử dụng các giao thức HTTP và HTTPS để liên lạc.Nó sử dụng các giao thức HTTP, HTTPS, FTP, FTPS.
    Nó có thể được sử dụng để đọc nội dung tệp.Nó có thể được sử dụng để đọc, chỉnh sửa, cập nhật, xóa các tệp khỏi máy chủ.
    Chậm trong hoạt động.An toàn và nhanh chóng hoạt động.
    Dễ hiểu.Phức tạp để hiểu.