/** * It should have a way to read cookie files * * @return void * @author Christopher Cowan * @test **/ public function It_should_have_a_way_to_read_cookie_files() { $cookies = get_cookies_from_file(dirname(__FILE__) . '/lib/fixtures/cookies.txt'); $this->assertEquals(count($cookies), 2); $this->assertEquals($cookies[0]['domain'], 'example.com'); $this->assertEquals($cookies[0]['domain_only'], true); $this->assertEquals($cookies[0]['path'], '/'); $this->assertEquals($cookies[0]['secure'], false); $this->assertEquals($cookies[0]['expires'], 0); $this->assertEquals($cookies[0]['key'], 'test_cookie'); $this->assertEquals($cookies[0]['value'], 'test-value'); $this->assertEquals($cookies[1]['domain'], 'example.com'); $this->assertEquals($cookies[1]['domain_only'], false); $this->assertEquals($cookies[1]['path'], '/secure'); $this->assertEquals($cookies[1]['secure'], true); $this->assertEquals($cookies[1]['expires'], 1291648153); $this->assertEquals($cookies[1]['key'], 'secure_cookie'); $this->assertEquals($cookies[1]['value'], 'test-secure-value'); }
/** * This will make a web request and then return the response and headers as an associated array. * * @return array * @author Christopher Cowan **/ function make_request($url, $method = 'GET', $options = array()) { # instanciate the curl request $curl = curl_init($url); # set the default options curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLINFO_HEADER_OUT, 1); # set the cookie file curl_setopt($curl, CURLOPT_COOKIEFILE, option('cookie_file')); curl_setopt($curl, CURLOPT_COOKIEJAR, option('cookie_file')); # set the request method curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); # set cookies $cookies = get_cookies_from_file(option('cookie_file')); if (isset($options['cookies']) && is_array($options['cookies'])) { $cookies = array_merge($cookies, $options['cookies']); } set_cookies_in_file($cookies, option('cookie_file')); # set custom headers if (isset($options['headers']) && is_array($options['headers'])) { $headers = array(); foreach ($options['headers'] as $key => $val) { $headers[] = "{$key}: {$val}"; } curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } # post arguments if (isset($options['post_fields']) && is_array($options['post_fields'])) { curl_setopt($curl, CURLOPT_POSTFIELDS, $options['post_fields']); } # post body if (isset($options['post_body'])) { curl_setopt($curl, CURLOPT_POSTFIELDS, $options['post_body']); } # get the response $response = curl_exec($curl); # seperate the header form the body, also handle 2 response codes $buffer = preg_split('/\\r\\n\\r\\n/', $response); if (count($buffer) == 3) { $results = array('body' => $buffer[2]); $headers = $buffer[1]; preg_match('/\\r\\n\\r\\nHTTP\\/\\d\\.\\d\\s+(\\d+)/', $response, $matches); } else { $results = array('body' => $buffer[1]); $headers = $buffer[0]; preg_match('/HTTP\\/\\d\\.\\d\\s+(\\d+)/', $response, $matches); } $results['request'] = curl_getinfo($curl); $results['status_code'] = $results['request']['http_code']; curl_close($curl); # split up headers preg_match_all('/([^\\r\\n:]+):\\s+([^\\r\\n]+)/', $headers, $matches); foreach ($matches[1] as $key => $val) { if (isset($results['headers'][$val])) { if (!is_array($results['headers'][$val])) { $results['headers'][$val] = array($results['headers'][$val]); } $results['headers'][$val][] = $matches[2][$key]; } else { $results['headers'][$val] = $matches[2][$key]; } } # parse cookies $results['cookies'] = get_cookies_from_file(option('cookie_file')); /* if(isset($results['headers']['Set-Cookie'])) { $results['cookies'] = array(); if(is_array($results['headers']['Set-Cookie'])) { $set_cookies = $results['headers']['Set-Cookie']; } else { $set_cookies = array($results['headers']['Set-Cookie']); } foreach($set_cookies as $cookie) { preg_match('/^([^=]+)=([^;]+)/', $cookie, $matches); $results['cookies'][$matches[1]] = $matches[2]; } }*/ return $results; }