/** * It should have a way to set cookies in a cookie file * * @return void * @author Christopher Cowan * @test **/ public function It_should_have_a_way_to_set_cookies_in_a_cookie_file() { $file = tempnam('/tmp', 'cookie-test-file-'); echo "{$file}\n"; $cookies = array(array('domain' => 'example.com', 'domain_only' => true, 'path' => '/', 'secure' => false, 'expires' => 123456789, 'key' => 'my_test', 'value' => 'my_value'), array('domain' => 'example.com', 'domain_only' => false, 'path' => '/secure', 'secure' => true, 'expires' => 0, 'key' => 'my_secure_test', 'value' => 'my_secure_value')); set_cookies_in_file($cookies, $file); $fixture_contents = <<<COOKIEFILE # Netscape HTTP Cookie File # http://curl.haxx.se/rfc/cookie_spec.html # This file was generated by libcurl! Edit at your own risk. example.com\tTRUE\t/\tFALSE\t123456789\tmy_test\tmy_value example.com\tFALSE\t/secure\tTRUE\t0\tmy_secure_test\tmy_secure_value COOKIEFILE; $file_contents = file_get_contents($file); $this->assertEquals($file_contents, $fixture_contents); }
/** * 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; }