Skip to content

Instantly share code, notes, and snippets.

@daluu
Created August 23, 2012 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daluu/3433180 to your computer and use it in GitHub Desktop.
Save daluu/3433180 to your computer and use it in GitHub Desktop.
PHP example for file downloader with Selenium
<?
/**
* This creates the cookie header string for curl to send when making requests.
* It includes the (PHP) session ID among possibly a few other cookies. This is
* not for use with curl's built-in cookie JAR (CURLOPT_COOKIEJAR) and file
* handling (CURLOPT_COOKIEFILE) options. This is using the curl header
* (CURLOPT_COOKIE) with value of "name1=value1; name2=value2..."
* See http://hasin.me/2007/04/18/cookiejar-in-curl-it-sucks/ and
* http://en.wikipedia.org/wiki/HTTP_cookie for more details.
*
* The cookie header string is created by extracting cookies from the Selenium
* RC browser session for use by curl. Can be adapted for WebDriver
* @author David Luu
* @return cookie string to pass to curl_setopt($ch, CURLOPT_COOKIE, $returnValueHere);
*/
function createCurlSessionCookieFromSeleniumSession(){
//this function assumes the function has reference to
//Selenium RC object via PHPUnit-Selenium, modify as needed
//get Selenium browser session for use by curl
//replace PHPSESSID with equivalent session cookie you need
$session = $this->getCookieByName("PHPSESSID");
//extract any additional cookies you need...
//build cookie string
$cookieString = "PHPSESSID=".$session;
//append additional cookies as needed in this format
//"name1=value1; name2=value2..." where last cookie no need ";"
return $cookieString;
}
/**
* Use curl w/ session cookie to download a file by URL and save to temp file on disk
* @author David Luu
* @param $fileUrl
* @param $sessionCookie - get via createCurlSessionCookieFromSeleniumSession()
* @return temp file name of downloaded file OR exception if download fails
*/
function downloadFileByCurl($fileUrl, $sessionCookie){
//set up HTTP request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fileUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
/* we're not using curl built in cookie management as we need to
* use cookies from Selenium and it's a pain to parse/convert
* from Selenium cookie output to curl cookie file format
* and read http://hasin.me/2007/04/18/cookiejar-in-curl-it-sucks/
*/
//curl_setopt($ch, CURLOPT_COOKIEJAR, $this->myCookieFile);
//curl_setopt($ch, CURLOPT_COOKIEFILE, $this->myCookieFile);
curl_setopt($ch, CURLOPT_COOKIE, $sessionCookie);
//perform download of file from URL
$output = curl_exec($ch);
curl_close($ch);
//if download successful, write to file
if($output){
$tmpfname = tempnam("/tmp", "yourUniquePrefix");
$handle = fopen($tmpfname, "w");
fwrite($handle, $output);
fclose($handle);
return $tmpfname;
//don't forget to delete or "unlink()" file after working with it
}else{
throw new Exception("Failed to download file $fileUrl for further testing.");
//or die("Failed to download file $fileUrl for further testing.");
}
}
?>
@daluu
Copy link
Author

daluu commented Jan 16, 2013

Also, on Windows (and perhaps other OSes), you may have to "enable" curl for PHP. Be sure to check on that. On Windows, that's just uncommenting the config option for curl in PHP.ini. Read the PHP docs online for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment