Skip to content

Instantly share code, notes, and snippets.

@kurtpayne
Created February 6, 2013 17:39
Show Gist options
  • Save kurtpayne/4724289 to your computer and use it in GitHub Desktop.
Save kurtpayne/4724289 to your computer and use it in GitHub Desktop.
WordPress and cURL cookies
<?php
// Create a cookie file
require_once ABSPATH . 'wp-admin/includes/file.php';
$cookie_file = wp_tempnam();
// Create a callback to let curl save cookies there
$cookie_saver = function( $ch ) use ( $cookie_file ) {
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie_file );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $cookie_file );
};
add_action( 'http_api_curl', $cookie_saver );
// Disable other transports so WP uses curl
add_filter( 'use_fsockopen_transport', '__return_false', 55 );
add_filter( 'use_streams_transport', '__return_false', 55 );
// Turn off SSL verify (YMMV)
add_filter( 'https_ssl_verify', '__return_false', 45 );
// Fetch the page
$result = wp_remote_get( 'https://docs.google.com/forms/d/1bPgvlQ-zwdVVBmmLEC0YtUu-yf94rshDLbMPcUFbsCU/viewform', array(
'user-agent' => $_SERVER['HTTP_USER_AGENT'],
'redirection' => 10
) );
print_r( $result );
// Clean up
remove_filter( 'https_ssl_verify', '__return_false', 45 );
remove_filter( 'use_fsockopen_transport', '__return_false', 55 );
remove_filter( 'use_streams_transport', '__return_false', 55 );
@unlink( $cookie_file );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment