function call_method($method, $params) { $this->error_code = 0; $xml = $this->post_request($method, $params); $impl = new IsterXmlSimpleXMLImpl(); $sxml = $impl->load_string($xml); $result = array(); $children = $sxml->children(); $result = $this->convert_simplexml_to_array($children[0]); if ($GLOBALS['facebook_config']['debug']) { // output the raw xml and its corresponding php object, for debugging: print '<div style="margin: 10px 30px; padding: 5px; border: 2px solid black; background: gray; color: white; font-size: 12px; font-weight: bold;">'; $this->cur_id++; print $this->cur_id . ': Called ' . $method . ', show ' . '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'params\');">Params</a> | ' . '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'xml\');">XML</a> | ' . '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'php\');">PHP</a>'; print '<pre id="params' . $this->cur_id . '" style="display: none; overflow: auto;">' . print_r($params, true) . '</pre>'; print '<pre id="xml' . $this->cur_id . '" style="display: none; overflow: auto;">' . htmlspecialchars($xml) . '</pre>'; print '<pre id="php' . $this->cur_id . '" style="display: none; overflow: auto;">' . print_r($result, true) . '</pre>'; print '</div>'; } if (is_array($result) && isset($result['error_code'])) { $this->error_code = $result['error_code']; return null; } return $result; }
/** * Fetches and parses XML from Amazon for the given query. * @param string $query Query string containing variables to search Amazon for. Valid variables: $isbn, $title, $author * @return array Array containing each book's information. */ function query_amazon( $query ) { require_once dirname(__FILE__) . '/sha256.inc.php'; if (!function_exists('hmac')) { function hmac($key, $data, $hashfunc='sha256') { $blocksize=64; if (strlen($key) > $blocksize) $key=pack('H*', $hashfunc($key)); $key=str_pad($key, $blocksize, chr(0x00)); $ipad=str_repeat(chr(0x36), $blocksize); $opad=str_repeat(chr(0x5c), $blocksize); $hmac = pack('H*', $hashfunc(($key^$opad) . pack('H*', $hashfunc(($key^$ipad) . $data)))); return $hmac; } } global $item, $items; $options = get_option('nowReadingOptions'); $using_isbn = false; parse_str($query); if ( empty($isbn) && empty($title) && empty($author) ) return false; if ( !empty($isbn) ) $using_isbn = true; // Our query needs different vars depending on whether or not we're searching by ISBN, so build it here. if ( $using_isbn ) { $isbn = preg_replace('#([^0-9x]+)#i', '', $isbn); $query = "isbn:$isbn"; } else { $query=''; if ( !empty($title) ) $query = 'title:' . urlencode($title); if ( !empty($author) ) $query .= 'author:' . urlencode($author); } // these items MUST be set in the Options screen $AWSAccessKeyId = trim($options['AWSAccessKeyId']); $SecretAccessKey = trim($options['SecretAccessKey']); # // some paramters $method = "GET"; $host = "ecs.amazonaws".$options['domain']; $uri = "/onca/xml"; // additional parameters $params["Service"] = "AWSECommerceService"; // GMT timestamp $params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z"); // API version $params["Version"] = "2009-03-31"; $params["AssociateTag"] = urlencode($options['associate']); $params["Power"] = $query; $params["Operation"] = "ItemSearch"; $params["SearchIndex"] = "Books"; $params["ResponseGroup"] = "Request,Large,Images,AlternateVersions"; $params["AWSAccessKeyId"] = $AWSAccessKeyId; // Sort paramters ksort($params); // re-build the request $request = array(); foreach ($params as $parameter=>$value) { $parameter = str_replace("_", ".", $parameter); $parameter = str_replace("%7E", "~", rawurlencode($parameter)); $value = str_replace("%7E", "~", rawurlencode($value)); $request[] = $parameter . "=" . $value; } $request = implode("&", $request); $signatureString = $method . chr(10) . $host . chr(10) . $uri . chr(10) . $request; $signature = urlencode(base64_encode(hmac($SecretAccessKey, $signatureString))); $request = "http://" . $host . $uri . "?" . $request . "&Signature=" . $signature; // Fetch the XML using either Snoopy or cURL, depending on our options. if ( $options['httpLib'] == 'curl' ) { if ( !function_exists('curl_init') ) { return new WP_Error('curl-not-installed', __('cURL is not installed correctly.', NRTD)); } else { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Now Reading ' . NOW_READING_VERSION); curl_setopt($ch, CURLOPT_HEADER, 0); if ( !empty($options['proxyHost']) ) { $proxy = $options['proxyHost']; if ( !empty($options['proxyPort']) ) { $proxy .= ":{$options['proxyPort']}"; } curl_setopt($ch, CURLOPT_PROXY, $proxy); } $xmlString = curl_exec($ch); curl_close($ch); } } else { require_once ABSPATH . WPINC . '/class-snoopy.php'; $snoopy = new snoopy; $snoopy->agent = 'Now Reading Redux' . NOW_READING_VERSION; if ( !empty($options['proxyHost']) ) $snoopy->proxy_host = $options['proxyHost']; if ( !empty($options['proxyHost']) && !empty($options['proxyPort']) ) $snoopy->proxy_port = $options['proxyPort']; $snoopy->fetch($request); $xmlString = $snoopy->results; } if ( empty($xmlString) ) { do_action('nr_search_error', $query); echo ' <div id="message" class="error fade"> <p><strong>' . __("Oops!") . '</strong></p> <p>' . sprintf(__("For some reason, I couldn't search for your book on amazon%s.", NRTD), $options['domain']) . '</p> <p>' . __("Amazon's Web Services may be down, or there may be a problem with your server configuration.") . '</p> '; if ( $options['httpLib'] ) echo '<p>' . __("Try changing your HTTP Library setting to <strong>cURL</strong>.", NRTD) . '</p>'; echo ' </div> '; return false; } require_once 'xml/IsterXmlSimpleXMLImpl.php'; $impl = new IsterXmlSimpleXMLImpl; $xml = $impl->load_string($xmlString); if ( $options['debugMode'] ) robm_dump("Amazon Search XML:", htmlentities(str_replace(">", ">\n", $xmlString))); $items = $xml->ItemSearchResponse->Items->children(); if (count($items) == 0) { return false; } $results = array(); foreach ($items as $item) { $attr = $item->ItemAttributes; if (!$attr) { continue; } $asin = $item->ASIN->CDATA(); if (empty($asin)) { continue; } // Get full meta-data given the current ISBN. Used to get all editions. $metaData = getMetadataFromIsbn($asin, $AWSAccessKeyId, $SecretAccessKey, urlencode($options['associate'])); if ($options['debugMode']) { robm_dump("Amazon Lookup XML:", htmlentities(str_replace(">", ">\n", $metaData))); } $metaDataParser = new IsterXmlSimpleXMLImpl; $metaDataXml = $metaDataParser->load_string($metaData); if (isset($metadata->ItemLookupResponse->Items->Request->Errors)) { continue;//$metadata->ItemLookupResponse->Items->Request->Errors; } $editions = $metaDataXml->ItemLookupResponse->Items->children(); if (count($editions) == 0) { continue; } // For each edition, add an entry. foreach ($editions as $edition) { if (!isset($edition->ASIN)) { continue; } $asin = $edition->ASIN->CDATA(); if (empty($asin)) { continue; } $title = $edition->ItemAttributes->Title->CDATA(); if (empty($title)) { continue; } $author = ''; if (is_array($edition->ItemAttributes->Author)) { foreach ($edition->ItemAttributes->Author as $a) { if (is_object($a)) { $author .= $a->CDATA() . ', '; } } $author = substr($author, 0, -2); } else { if (is_object($edition->ItemAttributes->Author)) { $author = $edition->ItemAttributes->Author->CDATA(); } } if (empty($author)) { $author = apply_filters('default_book_author', 'Unknown'); } $size = "{$options['imageSize']}Image"; if (empty($item->$size)) { continue; } $image = $item->$size->URL->CDATA(); if (empty($image)) { $image = get_option('siteurl') . '/wp-content/plugins/now-reading-redux/no-image.png'; } $binding = ''; if (isset($edition->ItemAttributes->Binding)) { $binding = $edition->ItemAttributes->Binding->CDATA(); } $ed = ''; if (isset($edition->ItemAttributes->Edition)) { $ed = $edition->ItemAttributes->Edition->CDATA(); } $date = ''; if (isset($edition->ItemAttributes->PublicationDate)) { $date = $edition->ItemAttributes->PublicationDate->CDATA(); } $publisher = ''; if (isset($edition->ItemAttributes->Publisher)) { $publisher = $edition->ItemAttributes->Publisher->CDATA(); } if ($options['debugMode']) { robm_dump("book:", $author, $title, $binding, $ed, $date, $publisher, $asin); } $results[] = apply_filters('raw_amazon_results', compact('author', 'title', 'binding', 'ed', 'date', 'publisher', 'image', 'asin')); } } $results = apply_filters('returned_books', $results); return $results; }
function getXMLData($search, $exact = false) { // Snoopy is used to robot the URL fetching include_once $include_path . "lib/snoopy.class.php"; $snoopy_retry = 3; $amazon_key = '19B1FW4R5ABSKBWNV582'; if ($exact) { $baseSearch = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&ResponseGroup=Large&Operation=ItemLookup&AWSAccessKeyId=' . $amazon_key . '&ItemId=' . $search; } else { $search_url = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&ResponseGroup=Large&Operation=ItemSearch&SearchIndex=Music&AWSAccessKeyId=' . $amazon_key; $baseSearch = $search_url . '&' . $search; } $snoopy = new Snoopy(); $snoopy_tries = 0; while ($snoopy_retry > $snoopy_tries) { $snoopy->fetch($baseSearch); $snoopy_tries++; if ($snoopy->status == 200) { $xml_content = $snoopy->results; break; } else { if ($snoopy->status) { print "\n<div width=\"100%\" align=\"center\" style=\"background:#C00000;color:#FFFFFF;padding:3px\"><b>"; print "\nThere was a problem fetching results from Amazon: <font color=\"red\">" . $snoopy->status . " " . $snoopy->error . "\n"; print "\nWe will retry this request " . ($snoopy_retry - $snoopy_tries) . " more times."; print "\n</font></b></div>"; //print "\n<div width=\"100%\" align=\"center\" style=\"background:#C08000;color:#FFFFFF;padding:3px\">We will retry this request " . $snoopy_retry - $snoopy_tries . " more times.</div>"; } else { print "<b>There was a fatal error: <font color=\"red\">" . $php_errormsg . "</font></b><br>"; return false; } } } // Amazon returns XML, so parse it through simpleXML if (isset($xml_content) && $xml_content != '') { if (!$test_php4 && extension_loaded('simplexml')) { $xml = new SimpleXMLElement($xml_content); } elseif (extension_loaded('xml')) { if (version_compare('5.0.0', phpversion())) { print "<div width=\"100%\" align=\"center\" style=\"background:#C08000;color:#FFFFFF;padding:3px\"><b>PHP5 supports SimpleXML in a native library. This function will run much faster if you enable this component on your server.</b></div>"; } include_once $include_path . "lib/simplexml/IsterXmlSimpleXMLImpl.php"; $parser = new IsterXmlSimpleXMLImpl(); $xml1 = $parser->load_string($xml_content); $xml = $xml1->ItemSearchResponse; } else { print "This metadata system requires PHP with the SimpleXML or plain libexpat XML extension installed."; exit; } if (empty($xml)) { print "<div width=\"100%\" align=\"center\" style=\"background:#C00000;color:#FFFFFF;padding:3px\"><b>There was a problem fetching results from Amazon: <font color=\"red\">" . $snoopy->status . " " . $snoopy->error . "</font></b></div>"; #print "<div width=\"100%\" align=\"center\" style=\"background:#C00000;color:#FFFFFF;padding:3px\">If you wish, you can report this error to the developer by clicking the following button."; #print '<form method="POST" action="http://www.darkhart.net/support.php"><input type="hidden" name="subject" value="JZ Custom - XML parse error"><input type="hidden" name="data" value="' . . '"><input type="submit" value="Submit Report"></form></div>'; print "<code>htmlentities({$xml_content})</code>"; return false; } } return $xml; }