⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo.php.txt

📁 google的gdata api包
💻 TXT
📖 第 1 页 / 共 2 页
字号:
<?php/* Our Google Base API developer key. */$developerKey = "REPLACE WITH YOUR DEVELOPER KEY";/* The items feed URL, used for queries, insertions and batch commands. */$itemsFeedURL = "http://www.google.com/base/feeds/items";/* Parsed recipe entries from a query. */$parsedEntries = array();/* Are we currently parsing an XML ENTRY tag? */$foundEntry = false;/* Current XML element being processed. */$curElement = "";/* Types of cuisine the user may select when inserting a recipe. */$cuisines = array('African', 'American', 'Asian', 'Caribbean', 'Chinese',  'French', 'Greek', 'Indian', 'Italian', 'Japanese', 'Jewish',   'Mediterranean', 'Mexican', 'Middle Eastern', 'Moroccan',   'North American', 'Spanish', 'Thai', 'Vietnamese', 'Other');/** * Creates the XML content used to insert a new recipe. */function buildInsertXML() {  $result = "<?xml version='1.0'?>" . "\n";  $result .= "<entry xmlns='http://www.w3.org/2005/Atom'" .    " xmlns:g='http://base.google.com/ns/1.0'>" . "\n";  $result .= "<category scheme='http://base.google.com/categories/itemtypes'" .    " term='Recipes'/>" . "\n";  $result .= "<title type='text'>" . $_POST['recipe_title'] . "</title>" .    "\n";  $result .= "<g:cuisine>" . $_POST['cuisine'] . "</g:cuisine>" . "\n";  $result .= "<g:item_type type='text'>Recipes</g:item_type>" . "\n";  $result .= "<g:cooking_time type='intUnit'>" . $_POST['time_val'] .             " " . $_POST['time_units'] . "</g:cooking_time>" . "\n";  $result .= "<g:main_ingredient type='text'>" . $_POST['main_ingredient'] .    "</g:main_ingredient>" . "\n";  $result .= "<g:serving_count type='number'>" . $_POST['serves'] .    "</g:serving_count>" . "\n";  $result .= "<content>" . $_POST['recipe_text'] . "</content>" . "\n";  $result .= "</entry>" . "\n";  return $result;}/** * Creates the XML content used to perform a batch delete. */function buildBatchXML() {  $counter = 0;  $result =  '<?xml version="1.0" encoding="UTF-8"?>' . "\n";  $result .= '<feed xmlns="http://www.w3.org/2005/Atom"' . "\n";  $result .= ' xmlns:g="http://base.google.com/ns/1.0"' . "\n";  $result .= ' xmlns:batch="http://schemas.google.com/gdata/batch">' . "\n";  foreach($_POST as $key => $value) {    if(substr($key, 0, 5) == "link_") {      $counter++;      $result .= '<entry>' . "\n";      $result .= '<id>' . $value . '</id>' . "\n";      $result .= '<batch:operation type="delete"/>' . "\n";      $result .= '<batch:id>' . $counter . '</batch:id>' . "\n";      $result .= '</entry>' . "\n";    }  }  $result .= '</feed>' . "\n";  return $result;}/** * Exchanges the given single-use token for a session * token using AuthSubSessionToken, and returns the result. */function exchangeToken($token) {  $ch = curl_init();    /* Create a CURL handle. */  curl_setopt($ch, CURLOPT_URL,    "https://www.google.com/accounts/AuthSubSessionToken");  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  curl_setopt($ch, CURLOPT_FAILONERROR, true);  curl_setopt($ch, CURLOPT_HTTPHEADER, array(    'Authorization: AuthSub token="' . $token . '"'  ));  $result = curl_exec($ch);  /* Execute the HTTP command. */  curl_close($ch);  $splitStr = split("=", $result);  return trim($splitStr[1]);}/** * Performs a query for all of the user's items using the * items feed, then parses the resulting XML with the * startElement, endElement and characterData functions * (below). */function getItems($token) {  $ch = curl_init();    /* Create a CURL handle. */  global $developerKey, $itemsFeedURL;  curl_setopt($ch, CURLOPT_URL, $itemsFeedURL . "?");  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  curl_setopt($ch, CURLOPT_HTTPHEADER, array(    'Content-Type: application/atom+xml',    'Authorization: AuthSub token="' . trim($token) . '"',    'X-Google-Key: key=' . $developerKey  ));  $result = curl_exec($ch);  /* Execute the HTTP command. */  curl_close($ch);  /* Parse the resulting XML. */  $xml_parser = xml_parser_create();  xml_set_element_handler($xml_parser, "startElement", "endElement");  xml_set_character_data_handler($xml_parser, "characterData");   xml_parse($xml_parser, $result);  xml_parser_free($xml_parser);}/** * Inserts a new recipe by performing an HTTP POST to the * items feed. */function postItem() {  $ch = curl_init();    /* Create a CURL handle. */  global $developerKey, $itemsFeedURL;  /* Set cURL options. */  curl_setopt($ch, CURLOPT_URL, $itemsFeedURL);  curl_setopt($ch, CURLOPT_POST, true);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  curl_setopt($ch, CURLOPT_FAILONERROR, true);  curl_setopt($ch, CURLOPT_HTTPHEADER, array(    'Authorization: AuthSub token="' . $_POST['token'] . '"',    'X-Google-Key: key=' . $developerKey,    'Content-Type: application/atom+xml'  ));  curl_setopt($ch, CURLOPT_POSTFIELDS, buildInsertXML());  $result = curl_exec($ch);  /* Execute the HTTP request. */  curl_close($ch);           /* Close the cURL handle. */  return $result;}/** * Updates an existing recipe by performing an HTTP PUT * on its feed URI, using the updated values a PUT data. */function updateItem() {  $ch = curl_init();    /* Create a CURL handle. */  global $developerKey;  /* Prepare the data for HTTP PUT. */  $putString = buildInsertXML();  $putData = tmpfile();  fwrite($putData, $putString);  fseek($putData, 0);  /* Set cURL options. */  curl_setopt($ch, CURLOPT_URL, $_POST['link']);  curl_setopt($ch, CURLOPT_PUT, true);  curl_setopt($ch, CURLOPT_INFILE, $putData);  curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString));  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  curl_setopt($ch, CURLOPT_FAILONERROR, true);  curl_setopt($ch, CURLOPT_HTTPHEADER, array(    'Authorization: AuthSub token="' . $_POST['token'] . '"',    'X-Google-Key: key=' . $developerKey,    'Content-Type: application/atom+xml'  ));  $result = curl_exec($ch);  /* Execute the HTTP request. */  fclose($putData);          /* Close and delete the temp file. */  curl_close($ch);           /* Close the cURL handle. */  return $result;}/** * Deletes a recipe by performing an HTTP DELETE (a custom * cURL request) on its feed URI. */function deleteItem() {  $ch = curl_init();  global $developerKey;    /* Set cURL options. */  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");  curl_setopt($ch, CURLOPT_URL, $_POST['link']);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  curl_setopt($ch, CURLOPT_FAILONERROR, true);  curl_setopt($ch, CURLOPT_HTTPHEADER, array(    'Authorization: AuthSub token="' . $_POST['token'] . '"',    'X-Google-Key: key=' . $developerKey  ));  $result = curl_exec($ch);  /* Execute the HTTP request. */  curl_close($ch);           /* Close the cURL handle.    */  return $result;}/** * Deletes all recipes by performing an HTTP POST to the * batch URI. */function batchDelete() {  $ch = curl_init();    /* Create a CURL handle. */  global $developerKey, $itemsFeedURL;  /* Set cURL options. */  curl_setopt($ch, CURLOPT_URL, $itemsFeedURL . "/batch");  curl_setopt($ch, CURLOPT_POST, true);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  curl_setopt($ch, CURLOPT_FAILONERROR, true);  curl_setopt($ch, CURLOPT_HTTPHEADER, array(    'Authorization: AuthSub token="' . $_POST['token'] . '"',    'X-Google-Key: key=' . $developerKey,    'Content-Type: application/atom+xml'  ));  curl_setopt($ch, CURLOPT_POSTFIELDS, buildBatchXML());  $result = curl_exec($ch);  /* Execute the HTTP request. */  curl_close($ch);           /* Close the cURL handle.    */  return $result;}/** * Callback function for XML start tags parsed by * xml_parse. */function startElement($parser, $name, $attrs) {  global $curElement, $foundEntry, $parsedEntries;  $curElement = $name;  if($curElement == "ENTRY") {    $foundEntry = true;    $parsedEntries[count($parsedEntries)] = array();  } else if($foundEntry && $curElement == "LINK") {    $parsedEntries[count($parsedEntries) - 1][$attrs["REL"]] = $attrs["HREF"];  }}/** * Callback function for XML end tags parsed by * xml_parse. */function endElement($parser, $name) {  global $curElement, $foundEntry, $parsedEntries;  if($name == "ENTRY") {    $foundEntry = false;  }}/** * Callback function for XML character data parsed by * xml_parse. */function characterData($parser, $data) {  global $curElement, $foundEntry, $parsedEntries;  if($foundEntry) {    $parsedEntries[count($parsedEntries) - 1][strtolower($curElement)] = $data;  }}/** * We arrive here when the user first comes to the form. The first step is * to have them get a single-use token. */function showIntroPage() {  global $itemsFeedURL;  $next_url  = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];  $redirect_url = 'https://www.google.com/accounts/AuthSubRequest?session=1';  $redirect_url .= '&next=';  $redirect_url .= urlencode($next_url);  $redirect_url .= "&scope=";  $redirect_url .= urlencode($itemsFeedURL);  print '<html>' . "\n";  print '<head><title>PHP Demo: Google Base data API</title>' . "\n";  print '<link rel="stylesheet" type="text/css" href="../../../css/dev_docs.css">' . "\n";  print '</head>' . "\n";  print '<body><center>' . "\n";  print '<table style="width:50%;">' . "\n";  print '<tr>' . "\n";  print '<th colspan="2" style="text-align:center;">PHP Demo: Google Base data API</th>' . "\n";  print '</tr>' . "\n";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -