Skip to content

Instantly share code, notes, and snippets.

@jsmarkus
Created August 23, 2012 14:08
Show Gist options
  • Save jsmarkus/3437006 to your computer and use it in GitHub Desktop.
Save jsmarkus/3437006 to your computer and use it in GitHub Desktop.
<?php
class GReader {
protected $auth;
public function __construct($login, $password) {
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => $login,
"Passwd" => $password,
"service" => "writely",
"source" => "my-fake-app"
);
// Initialize the curl object
$curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute
$response = curl_exec($curl);
// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$this->auth = $matches[1];
curl_close($curl);
}
protected function createParser($options) {
$sax = xml_parser_create();
xml_parser_set_option($sax, XML_OPTION_CASE_FOLDING, false);
xml_parser_set_option($sax, XML_OPTION_SKIP_WHITE, true);
xml_set_element_handler($sax, $options['onStart'], $options['onEnd']);
return $sax;
}
public function request($url, $method, $params=array()) {
$curl = curl_init($url);
$parserOptions = array(
'onStart'=>function () {
var_dump('START:');
var_dump(func_get_args());
},
'onEnd'=>function () {
var_dump('END:');
var_dump(func_get_args());
}
);
$ctx = stream_context_create(array(
'gxml'=>array(
'parser'=>$this->createParser($parserOptions)
)));
$fp = fopen("gxml://nothing", "r+", false, $ctx);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_POST, $method==='post');
$headers = array(
"Authorization: GoogleLogin auth=" . $this->auth,
"GData-Version: 3.0",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
fclose($fp);
return $response;
}
}
class GXMLStream {
protected $buffer;
public $context;
function stream_open($path, $mode, $options, &$opened_path) {
// Has to be declared, it seems...
var_dump('Opening');
$ctx = $this->context;
$options = stream_context_get_options($ctx);
var_dump($options);
$this->parser = $options['gxml']['parser'];
return true;
}
public function stream_write($data) {
// var_dump('data:');
// var_dump($data);
xml_parse($this->parser, $data);
return strlen($data);
}
}
stream_wrapper_register("gxml", "GXMLStream")
or die("Failed to register protocol");
<?php
require_once('GReader.php');
$gr = new GReader('my@user.name', 'P455\/\/0rD');
$gr->request('https://docs.google.com/feeds/default/private/full', 'get');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment