PHP cuộn tròn trong vòng lặp

Hãy lấy một ví dụ về nơi chúng tôi cần tìm nạp các bản ghi từ máy chủ dựa trên id, giả sử rằng máy chủ sẽ trả về dữ liệu cho một id trong một yêu cầu

Bây giờ, nếu chúng tôi sử dụng cURL thì chúng tôi cần lặp lại vòng lặp cho từng id và máy chủ yêu cầu, do đó, nếu có 10 id thì thời gian phản hồi trung bình sẽ gấp 10 lần một yêu cầu

Tôi khá chắc chắn rằng các url là chính xác và được cắt xén chính xác cũng như độ dài tiêu đề giống nhau. Nếu tôi đặt một vài url cuối cùng này lên đầu danh sách để loại bỏ, thì nó sẽ hoạt động, nhưng một vài url cuối cùng của danh sách lại gặp lỗi 400 Yêu cầu không hợp lệ. Điều gì có thể là vấn đề?

Lời khuyên nào?

Một cái gì đó như dưới đây

for[$i=0;$i > sizeof[$url];$i++]    
$data[$i] = $this->get[$url[$i]]; 



function get[$url] {

$this->headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8, image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
        $this->headers[] = 'Connection: Keep-Alive';
        $this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
        $this->user_agent = 'Mozilla/5.0 [Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12] Gecko/20101026 Firefox/3.6.12 [.NET CLR 3.5.30729]';

set_time_limit[EXECUTION_TIME_LIMIT];
        $default_exec_time = ini_get['max_execution_time'];

        $this->redirectcount = 0;
        $process = curl_init[$url];
        curl_setopt[$process, CURLOPT_HTTPHEADER, $this->headers];
        curl_setopt[$process, CURLOPT_HEADER, 1];
        curl_setopt[$process, CURLOPT_USERAGENT, $this->user_agent];
        if [$this->cookies == TRUE] curl_setopt[$process, CURLOPT_COOKIEFILE, $this->cookie_file];
        if [$this->cookies == TRUE] curl_setopt[$process, CURLOPT_COOKIEJAR, $this->cookie_file];

        //off compression for debugging's sake
        //curl_setopt[$process,CURLOPT_ENCODING , $this->compression];

        curl_setopt[$process, CURLOPT_TIMEOUT, 180];
        if [$this->proxy] curl_setopt[$process, CURLOPT_PROXY, $this->proxy];
        if [$this->proxyauth]{ 
            curl_setopt[$process, CURLOPT_HTTPPROXYTUNNEL, 1]; 
            curl_setopt[$process, CURLOPT_PROXYUSERPWD, $this->proxyauth];  
         }
        curl_setopt[$process, CURLOPT_RETURNTRANSFER, 1];
        curl_setopt[$process, CURLOPT_FOLLOWLOCATION, TRUE];
        curl_setopt[$process,CURLOPT_MAXREDIRS,10]; 

        //added
        //curl_setopt[$process, CURLOPT_AUTOREFERER, 1];
        curl_setopt[$process,CURLOPT_VERBOSE,TRUE];
        if [$this->referrer] curl_setopt[$process,CURLOPT_REFERER,$this->referrer];

        if[$this->cookies]{
            foreach[$this->cookies as $cookie]{
                curl_setopt [$process, CURLOPT_COOKIE, $cookie];
                //echo $cookie; 
            }
        }

        $return = $this->redirect_exec[$process];//curl_exec[$process] or curl_error[$process];
        curl_close[$process];
        set_time_limit[$default_exec_time];//setback to default

        return $return;
    }

    function redirect_exec[$ch, $curlopt_header = false] {

    //curl_setopt[$ch, CURLOPT_HEADER, true];
    //curl_setopt[$ch, CURLOPT_RETURNTRANSFER, true];
    $data = curl_exec[$ch];
    $file = fopen[DP_SCRAPE_DATA_CURL_DIR.$this->redirectcount.".html","w"];
    fwrite[$file,$data];
    fclose[$file];

    $info =    curl_getinfo[$ch];
    print_r[$info];echo "
"; $http_code = $info['http_code']; if [$http_code == 301 || $http_code == 302 || $http_code == 303] { //list[$header] = explode["\r\n\r\n", $data]; //print_r[$header]; $matches = array[]; //print_r[$data]; //Check if the response has a Location to redirect to preg_match['/[Location:|URI:][.*?]\n/', $data, $matches]; $url = trim[array_pop[$matches]]; //print_r[$url]; $url_parsed = parse_url[$url]; //print_r[$url_parsed]; if [isset[$url_parsed['path']] && isset[$url] && !empty[$url] ] { //echo "
".$url; curl_setopt[$ch, CURLOPT_URL, MY_HOST.$url]; //echo "
".$url; $this->redirectcount++; return $this->redirect_exec[$ch]; //return $this->get[MY_HOST.$url]; //$this->redirect_exec[$ch]; } } elseif[$http_code == 200]{ $matches = array[]; preg_match['/[/i', $data, $matches]; //print_r[$matches]; $url = trim[array_pop[$matches]]; //print_r[$url]; $url_parsed = parse_url[$url]; //print_r[$url_parsed]; if [isset[$url_parsed['path']] && isset[$url] && !empty[$url] ] { curl_setopt[$ch, CURLOPT_URL, $url]; //echo "
".$url; $this->redirectcount++; sleep[SLEEP_INTERVAL]; return $this->redirect_exec[$ch]; //return $this->get[$url]; //$this->redirect_exec[$ch]; } } //echo "data ".$data; $this->redirectcount++; return $data ; // $info['url']; }

trong đó $urls là tất cả các url chứa tất cả chuỗi truy vấn cho yêu cầu nhận

tôi nhận ra từ curl_getinfo , [request_size ] ngày càng lớn hơn mà lẽ ra nó không nên như vậy. nó phải có cùng kích thước. Làm cách nào tôi có thể in/lặp lại thông tin yêu cầu http của mình để gỡ lỗi?

Tôi có thể sử dụng cURL trong php không?

Công dụng của cURL trong PHP . cURL giúp kết nối giữa các trang web và tên miền khác nhau trở nên đơn giản. Lấy một bản sao của tài liệu của một trang web. Gửi biểu mẫu tự động, xác thực và sử dụng cookie. cURL is a PHP extension that 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.

Làm cách nào để lấy URL cURL trong php?

curl_setopt[$ch, CURLOPT_URL, $url] Nó truyền URL dưới dạng tham số, url đích này trả về mà bạn muốn lấy từ internet. curl_setopt[$ch, CURLOPT_RETURNTRANSFER, TRUE] Nó được đặt thành TRUE để trả về chuyển dưới dạng một chuỗi giá trị trả về của curl_exec[] thay vì xuất trực tiếp.

Các tùy chọn cURL trong php là gì?

Hàm cURL ¶ .
curl_close — Đóng phiên cURL
curl_copy_handle - Sao chép một điều khiển cURL cùng với tất cả các tùy chọn của nó
curl_errno - Trả về số lỗi cuối cùng
curl_error - Trả về chuỗi chứa lỗi cuối cùng cho phiên hiện tại
curl_escape — URL mã hóa chuỗi đã cho
curl_exec - Thực hiện phiên cURL

php cURL có đồng bộ không?

Câu trả lời ngắn gọn là không, nó không phải là không đồng bộ . Câu trả lời dài hơn là "Không trừ khi bạn tự viết phần phụ trợ để làm như vậy. " Nếu bạn đang sử dụng XHR, mỗi yêu cầu sẽ có một luồng công nhân khác nhau trên phần phụ trợ, điều đó có nghĩa là không có yêu cầu nào chặn bất kỳ yêu cầu nào khác, trừ quá trình nhấn và giới hạn bộ nhớ.

Chủ Đề