Hướng dẫn curl get redirect url php - curl lấy url chuyển hướng php

Ở đây tôi nhận được các tiêu đề HTTP tài nguyên sau đó tôi phân tích các tiêu đề ra một mảng $ retval. Tôi đã nhận được mã để phân tích các tiêu đề từ đây (http://www.bhootnath.in/blog/2010/10/parse-http mounders-in-php/) Bạn cũng có thể sử dụng http://php.net/ Hướng dẫn sử dụng/en/function.http-parse-meaders.php Nếu bạn có (PECL PECL_HTTP> = 0.10.0)

        $ch = curl_init();
        $timeout = 0;
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        // Getting binary data
        $header = curl_exec($ch);
        $retVal = array();
        $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
        foreach( $fields as $field ) {
            if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
                $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
                if( isset($retVal[$match[1]]) ) {
                    $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
                } else {
                    $retVal[$match[1]] = trim($match[2]);
                }
            }
        }
//here is the header info parsed out
echo '
';
print_r($retVal);
echo '
'; //here is the redirect if (isset($retVal['Location'])){ echo $retVal['Location']; } else { //keep in mind that if it is a direct link to the image the location header will be missing echo $_GET[$urlKey]; } curl_close($ch);

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận
    Basic Function used in cURL:

    • Nói chung, Curl là viết tắt của ‘máy khách cho URL, ở đây URL được viết bằng chữ hoa để chỉ ra rằng Curl liên quan đến URL. It will initiate the curl a new session and return a cURL handle.
      Syntax:
      curl_init();
    • Phương pháp PHP: Hàm cơ bản được sử dụng trong Curl: It sets an option for a cURL session identified by the ch parameter. Option specifies which option is to set, and value specifies the value for the given option.
      Syntax:
      curl_setopt($ch, option, value);
      

    hàm curl_init ()

    • Hàm Curl_SetOpt (): Nó đặt một tùy chọn cho phiên Curl được xác định bởi tham số CH. Tùy chọn Chỉ định tùy chọn nào được đặt và giá trị Chỉ định giá trị cho tùy chọn đã cho.SyNTAX: ________ 2 It pass URL as a parameter, this return target url that you want to get from the internet.
    • Tham số với các lệnh quan trọng khác: It is set to TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
    • curl_setopt ($ CH, curlopt_url, $ url) Nó chuyển URL dưới dạng tham số, URL mục tiêu trả về này mà bạn muốn nhận từ Internet. It is set to TRUE to follow any “Location: ” header that the server sends as part of the HTTP header.
    • curl_setopt ($ CH, curlopt_returntransfer, true) Nó được đặt thành đúng để trả về việc chuyển thành một chuỗi của giá trị trả về của curl_exec () thay vì xuất trực tiếp. It passed to get cURL info of effective URL.
    • Curl_setopt ($ CH, curlopt_followLocation, true) Nó được đặt thành đúng để làm theo bất kỳ vị trí nào của người dùng: Tiêu đề mà máy chủ gửi như một phần của tiêu đề HTTP. It grab URL and pass it to the variable for showing output.
    • Curl_GetInfo ($ CH, curlinfo_effective_url) Nó đã thông qua để có được thông tin Curl của URL hiệu quả. It close curl resource, and free up system resources.

    Program:

    curl_init();
    1

    Curl_exec ($ CH) Nó lấy URL và chuyển nó đến biến để hiển thị đầu ra.

    curl_init();
    4
    curl_init();
    2
    curl_init();
    6
    curl_init();
    7
    curl_init();
    8

    curl_init();
    4
    curl_init();
    2
    curl_setopt($ch, option, value);
    
    1

    curl_init();
    4
    curl_init();
    2
    curl_setopt($ch, option, value);
    
    4

    Curl_close ($ CH) Nó đóng tài nguyên Curl và giải phóng tài nguyên hệ thống.

    curl_init();
    2
    curl_init();
    3

    Original URL: https://geeksforgeeks.org/
    Redirected URL: https://www.geeksforgeeks.org/
    
    3
    curl_init();
    2
    curl_init();
    8

    curl_setopt($ch, option, value);
    
    5
    curl_setopt($ch, option, value);
    
    6
    curl_init();
    2

    curl_setopt($ch, option, value);
    
    9
    Original URL: https://geeksforgeeks.org/
    Redirected URL: https://www.geeksforgeeks.org/
    
    0__12

    curl -Ls -w %{url_effective} -o /dev/null 
    https://www.geeksforgeeks.org/php-cucrl/
    
    0

    Output:

    Original URL: https://geeksforgeeks.org/
    Redirected URL: https://www.geeksforgeeks.org/
    

    Original URL: https://geeksforgeeks.org/
    Redirected URL: https://www.geeksforgeeks.org/
    
    6
    Original URL: https://geeksforgeeks.org/
    Redirected URL: https://www.geeksforgeeks.org/
    
    7
    Original URL: https://geeksforgeeks.org/
    Redirected URL: https://www.geeksforgeeks.org/
    
    8
    curl_init();
    7
    Original URL: https://geeksforgeeks.org/
    Redirected URL: https://www.geeksforgeeks.org/
    
    8
    curl -Ls -w %{url_effective} -o /dev/null [URL]
    
    1
    curl -Ls -w %{url_effective} -o /dev/null [URL]
    
    2

    1. Original URL: https://geeksforgeeks.org/
      Redirected URL: https://www.geeksforgeeks.org/
      
      6
      curl -Ls -w %{url_effective} -o /dev/null [URL]
      
      4
      Original URL: https://geeksforgeeks.org/
      Redirected URL: https://www.geeksforgeeks.org/
      
      8
      curl_setopt($ch, option, value);
      
      9
      Original URL: https://geeksforgeeks.org/
      Redirected URL: https://www.geeksforgeeks.org/
      
      8
      curl -Ls -w %{url_effective} -o /dev/null [URL]
      
      1
      curl -Ls -w %{url_effective} -o /dev/null [URL]
      
      2

      Syntax:
      curl -Ls -w %{url_effective} -o /dev/null [URL]
      

      Description:

      • Phương pháp tiếp cận dòng lệnh: — command name
      • Nhận URL chuyển hướng với Curl: Cú pháp: ________ 4 — Silent mode
      • Curl - Tên lệnh — Location which Follow redirects
      • -S -Chế độ im lặng — Dump headers here
      • -L -Vị trí theo chuyển hướng — URL that performs redirection
      • -D - -tiêu đề đổ tại đây — remove extra stdout info
      • [URL] - URL thực hiện chuyển hướng — final destination

      Ví dụ 1:

      curl -Ls -w %{url_effective} -o /dev/null 
      https://www.geeksforgeeks.org/php-cucrl/
      

      Output:

      Hướng dẫn curl get redirect url php - curl lấy url chuyển hướng php

    2. Thực hiện theo các chuyển hướng với Curl:

      syntax:

      curl -s -L -D - [URL] -o /dev/null -w '%{url_effective}'
      

      Ví dụ 2: Theo chuyển hướng đến trang Lỗi 404. Following redirect to 404 Error page.

      curl -s -L -D - 
      https://www.geeksforgeeks.org/php-cucrl/ -o /dev/null -w 
      '%{url_effective}'
      

      Output:

      D:\mycurl\bin>curl -s -L -D - https://www.geeksforgeeks.org/php-cucrl/ 
      -o /dev/null -w '%{url_effective}'
      HTTP/2 404
      server: Apache
      strict-transport-security: max-age=3600; includeSubDomains
      link: ; rel="https://api.w.org/"
      access-control-allow-credentials: true
      x-frame-options: DENY
      x-content-type-options: nosniff
      content-type: text/html; charset=UTF-8
      cache-control: must-revalidate, max-age=3, s-maxage=21600
      date: Mon, 08 Jul 2019 01:34:28 GMT
      
      'https://www.geeksforgeeks.org/php-cucrl/'
      
    3. Theo sau chuyển hướng sang chính xác trang: Ví dụ 3: ________ 9
      Example 3:
      curl -s -L -D - 
      https://www.geeksforgeeks.org/php-curl/ -o /dev/null -w 
      '%{url_effective}'
      

      Output:

      curl_init();
      0

    Làm thế nào tôi có thể nhận được URL chuyển hướng trong PHP?

    Trả lời: Sử dụng hàm tiêu đề PHP () Bạn có thể chỉ cần sử dụng hàm tiêu đề PHP () để chuyển hướng người dùng sang một trang khác. Mã PHP trong ví dụ sau sẽ chuyển hướng người dùng từ trang mà nó được đặt vào URL http://www.example.com/another-page.php. Bạn cũng có thể chỉ định URL tương đối.Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

    Làm thế nào để chuyển hướng đến một trang khác trong PHP bằng Curl?

    curl_setopt ($ CH, curlopt_url, $ url) Nó chuyển URL dưới dạng tham số, URL mục tiêu trả về này mà bạn muốn nhận từ Internet.curl_setopt ($ CH, curlopt_returntransfer, true) Nó được đặt thành đúng để trả về việc chuyển thành một chuỗi của giá trị trả về của curl_exec () thay vì xuất trực tiếp. It pass URL as a parameter, this return target url that you want to get from the internet. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE) It is set to TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.

    Curlopt_url là gì?

    Curlopt_url: Đây là URL mà bạn muốn PHP để tìm nạp.Bạn cũng có thể đặt tùy chọn này khi khởi tạo phiên với hàm curl_init ().Curlopt_userpwd: Truyền một chuỗi được định dạng theo cách [tên người dùng]: [Mật khẩu], để PHP sử dụng cho kết nối.the URL that you want PHP to fetch. You can also set this option when initializing a session with the curl_init() function. CURLOPT_USERPWD: Pass a string formatted in the [username]:[password] manner, for PHP to use for the connection.

    Việc sử dụng Curl trong PHP là gì?

    Curl là tiện ích mở rộng PHP cho phép bạn sử dụng cú pháp URL để nhận và gửi dữ liệu.Curl làm cho nó đơn giản để kết nối giữa các trang web và tên miền khác nhau.Có được một bản sao tài liệu của một trang web.Đệ trình các biểu mẫu tự động, xác thực và sử dụng cookie.allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains. Obtaining a copy of a website's material. Submission of forms automatically, authentication and cookie use.