note 50194 added to function.curl-setopt

From: Date: Mon, 21 Feb 2005 17:38:46 +0000
Subject: note 50194 added to function.curl-setopt
Groups: php.notes 
Request: Send a blank email to php-notes+get-85316@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. 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); ---- Manual Page -- http://www.php.net/manual/en/function.curl-setopt.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50194 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50194&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50194&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50194&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50194&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50194&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50194&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85316) next »