Resources for Developers Using Amazon's Product Advertising API
· Home  
· Search  
· Browse Nodes  
· Data Feed?  
· FAQs  
· One-Second Rule  
· PHP Examples  
· Tips for Associates  
· Tools  
· Understanding A2S  

Create a custom Amazon Associate Store in minutes with Associate-O-Matic.

Using CURL to Get Results from ECS

The following PHP function allows you to fetch results from an A2S query including the status code. It returns the text returned from the URL. It also returns the HTTP status code and the HTTP headers.

function getAmazonPage($url, &$code, &$headers) 
 { 
  $ch = curl_init(); 
  curl_setopt ($ch, CURLOPT_URL, $url); 
  curl_setopt ($ch, CURLOPT_USERAGENT, ' Your Name /1.0 (+ http://www. yourdomain.com /)'); 
  curl_setopt ($ch, CURLOPT_HEADER, 1); 
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
  curl_setopt ($ch, CURLOPT_TIMEOUT, 120); 
  $result = curl_exec ($ch); 
  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
  $hsize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 
  $headers = substr($result, 0, $hsize - 2); 
  $result = substr($result, $hsize); 
  curl_close($ch); 
  return $result; 
 } 
To use the function, do something like:

$result = getAmazonPage($rest_url, $code, $header); 
if ($code == 500 || $code == 503) 
  { 
   ... handle error
  }
 else
  {
   ... process XML
  }

Copyright © 2010 by Roger Smith