note 50194 modified in function.curl-setopt by danbrown

From: Date: Sun, 17 May 2009 17:33:00 +0000
Subject: note 50194 modified in function.curl-setopt by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154998@lists.php.net to get a copy of this message
Another way to automate a login process and store multiple cookies for additional transfers. (The easiest way is probably just to use CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE and save them to a file. But if you need to change those cookie values or add/delete them then you can use the code below to set them.) Continuing with Paul Ebermann's parsing function. <?php function parse_response($this_response) { // Split response into header and body sections list($response_headers, $response_body) = explode("\r\n\r\n", $this_response, 2); $response_header_lines = explode("\r\n", $response_headers); // First line of headers is the HTTP response code $http_response_line = array_shift($response_header_lines); if(preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line, $matches)) { $response_code = $matches[1]; } // put the rest of the headers in an array $response_header_array = array(); foreach($response_header_lines as $header_line) { list($header,$value) = explode(': ', $header_line, 2); $response_header_array[$header] .= $value."\n"; } return array("code" => $response_code, "header" => $response_header_array, "body" => $response_body); } // Do transfer, and make sure to include header in response $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "[login process]"); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); // Parse the response message $response = parse_response($response); // Create the basic header $this_header = array( "MIME-Version: 1.0", "Content-type: text/html; charset=iso-8859-1", "Content-transfer-encoding: text" ); // Add each cookie that has been returned in the response // If cookies need to be added/deleted or value changed, then add code here $cookies = explode("\n", $response["header"]["Set-Cookie"]); foreach($cookies as $this_cookie) { array_push($this_header, "Cookie: ".$this_cookie); } // Next set of transfers $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "[new transfer]"); curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); ?> --was-- Another way to automate a login process and store multiple cookies for additional transfers. (The easiest way is probably just to use CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE and save them to a file. But if you need to change those cookie values or add/delete them then you can use the code below to set them.) Continuing with Paul Ebermann's parsing function. function parse_response($this_response) { // Split response into header and body sections list($response_headers, $response_body) = explode("\r\n\r\n", $this_response, 2); $response_header_lines = explode("\r\n", $response_headers); // First line of headers is the HTTP response code $http_response_line = array_shift($response_header_lines); if(preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line, $matches)) { $response_code = $matches[1]; } // put the rest of the headers in an array $response_header_array = array(); foreach($response_header_lines as $header_line) { list($header,$value) = explode(': ', $header_line, 2); $response_header_array[$header] .= $value."\n"; } return array("code" => $response_code, "header" => $response_header_array, "body" => $response_body); } // Do transfer, and make sure to include header in response $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "[login process]"); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); // Parse the response message $response = parse_response($response); // Create the basic header $this_header = array( "MIME-Version: 1.0", "Content-type: text/html; charset=iso-8859-1", "Content-transfer-encoding: text" ); // Add each cookie that has been returned in the response // If cookies need to be added/deleted or value changed, then add code here $cookies = explode("\n", $response["header"]["Set-Cookie"]); foreach($cookies as $this_cookie) { array_push($this_header, "Cookie: ".$this_cookie); } // Next set of transfers $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "[new transfer]"); curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); http://php.net/manual/en/function.curl-setopt.php

« previous php.notes (#154998) next »