[ PHPXref.com ] [ Generated: Sun Jul 20 21:13:28 2008 ] [ Yahoolib 0.1 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> yahootools.inc.php (source)

   1  <?php
   2  
   3  /**

   4   * yahootools.inc.php -- Helpful functions for running Yahoo! REST requests

   5   *    

   6   * @package yahoolib

   7   * @author Jason Levitt. XML parsing function based on code by Torsten Koster   

   8   * @version 0.1 June 3rd, 2005

   9   * @link  http://sourceforge.net/projects/yahoolib

  10  */
  11  
  12  /**

  13   * Global variables

  14   *    

  15   * $xml_list_elements contains any V1 XML elements that are array structures

  16   * $parser_error, $getdata_error, and $postdata_error contain explanations

  17   * of error conditions for the XmlParser(), GetData(), and PostData() 

  18   * functions

  19   *

  20   * @global string $parser_error 

  21   * @global string $getdata_error 

  22   * @global string $postdata_error 

  23   * @global array  $xml_list_elements

  24   *

  25  */
  26  $parser_error='';
  27  $getdata_error='';
  28  $postdata_error='';
  29  $xml_list_elements = array(
  30  "Result"
  31  );
  32  
  33  /**

  34   * XmlParser -- parse XML into an associative array

  35   *

  36   * Takes a string of XML as input and returns an associative array. If

  37   * an error occurs, false is returned and the global $parser_error describes

  38   * the error in detail.

  39   *

  40   * @param string $string A string of XML

  41   * @return boolean|array  Returns false on error or associative array on success

  42   *

  43   */
  44  function XmlParser($string)
  45  {
  46      global $parser_error;
  47  
  48      $parser_error='';
  49      $values=array();
  50  
  51      // Create parser

  52      $p = xml_parser_create("UTF-8");
  53      xml_parser_set_option($p,XML_OPTION_CASE_FOLDING,false);
  54      xml_parser_set_option($p,XML_OPTION_SKIP_WHITE,true);
  55  
  56      // Parse into array structure

  57      $rc = xml_parse_into_struct($p,$string,$values);
  58  
  59      /* Check for Parsing Error */

  60      if (!$rc)
  61      {
  62          $errorcode = xml_get_error_code($p);
  63          $errstring = xml_error_string($errorcode);
  64          $byte= xml_get_current_byte_index($p);
  65          $parser_error = "XML PARSER ERROR: Error Code= $errorcode, Explanation= $errstring, Byte Number= $byte";
  66          xml_parser_free($p);
  67          return false;
  68      }
  69  
  70      xml_parser_free($p);
  71  
  72      // We store our path here

  73      $hash_stack = array();
  74  
  75      // This is our target

  76      $ret = array();
  77  
  78      foreach ($values as $val) {
  79  
  80          switch ($val['type']) {
  81              case 'open': // Start array structure
  82              array_push($hash_stack, $val['tag']);
  83              $valarg= (isset($val['attributes'])) ? $val['attributes'] : null;
  84              $ret = composeArray($ret, $hash_stack, $valarg);
  85              break;
  86  
  87              case 'close': // All done with this element
  88              array_pop($hash_stack);
  89              break;
  90  
  91              case 'complete':
  92              array_push($hash_stack, $val['tag']);
  93              $valarg=(isset($val['value']))? $val['value'] : null;
  94              // handle all attributes except those in 'open' container tags

  95              if (isset($val['attributes'])) {
  96                  $temparr=array($val['tag'] => $valarg);
  97                  $valarg=array_merge($val['attributes'], $temparr);
  98              };
  99              $ret = composeArray($ret, $hash_stack, $valarg);
 100              array_pop($hash_stack);
 101              break;
 102  
 103              default:
 104              // Ignoring CDATA type

 105          }
 106      }
 107  
 108      return $ret;
 109  }
 110  
 111  /**

 112   * A private helper function for XmlParser(). Parses repeated

 113   * XML values into arrays.

 114   *

 115   * @param array $array

 116   * @param array $elements

 117   * @param string $value

 118   * @return array

 119   */
 120  function &composeArray($array, $elements, $value)
 121  {
 122      global $xml_list_elements;
 123  
 124      // Get current element

 125      $element = array_shift($elements);
 126  
 127      // Does the current element refer to a list?

 128      if (in_array($element,$xml_list_elements))
 129      {
 130          // Are there more elements?

 131          if(sizeof($elements) > 0)
 132          {
 133              $array[$element][sizeof($array[$element])-1] = &composeArray($array[$element][sizeof($array[$element])-1], $elements, $value);
 134          }
 135          else // It's an array
 136          {
 137              $size = (isset($array[$element]))?  sizeof($array[$element]) : 0;
 138              $array[$element][$size] = $value;
 139          }
 140      }
 141      else
 142      {
 143          // Are there more elements?

 144          if(sizeof($elements) > 0)
 145          {
 146              $array[$element] = &composeArray($array[$element], $elements, $value);
 147          }
 148          else
 149          {
 150              $array[$element] = $value;
 151          }
 152      }
 153  
 154      return $array;
 155  }
 156  
 157  
 158  /**

 159   * GetData -- Fetch a URL using an HTTP 1.0 GET operation

 160   *

 161   * GetData returns a string that is the result of doing

 162   * an HTTP 1.0 GET request on the URL. If an error occurs, false

 163   * is returned and the global $getdata_error is set to describe the error.

 164   *

 165   * @param string $url A valid HTTP URL

 166   * @param integer $timeout Number of seconds to block waiting for a response

 167   * @return boolean|string  Either false on error or result of fetching the URL

 168   *

 169   */
 170  function GetData($url, $timeout) {
 171      global $getdata_error;
 172  
 173      // Parse the URL into parameters for fsockopen

 174      $UrlArr = parse_url($url);
 175      $host = $UrlArr['host'];
 176      $port = (isset($UrlArr['port'])) ? $UrlArr['port'] : 80;
 177      $path = $UrlArr['path'] . '?' . $UrlArr['query'];
 178  
 179      // Zero out the error response

 180      $errno = null;
 181      $errstr = '';
 182      $getdata_error = '';
 183      $errorflag = false;
 184  
 185      // Open the connection to Yahoo

 186      $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
 187  
 188      // Failed to open the URL

 189      if (!is_resource($fp)) {
 190          $getdata_error = "Fsockopen error number = $errno Details = $errstr";
 191          return false;
 192      }
 193  
 194      // Send an HTTP GET header and Host header

 195      if (!(fwrite($fp, 'GET '. $path .' HTTP/1.0' . "\r\n". 'Host: ' . $host . "\r\n\r\n"))) {
 196          fclose($fp);
 197          $getdata_error = "Fwrite error. Could not write GET and Host headers";
 198          return false;
 199      }
 200  
 201      // Block on the socket port, waiting for response from Yahoo

 202      if (function_exists('socket_set_timeout')) {
 203          @socket_set_timeout($fp, $timeout);
 204          socket_set_blocking($fp, true);
 205      }
 206  
 207      // Get the HTTP response code from Yahoo

 208      $line = fgets($fp , 1024);
 209  
 210      if ($line == false){
 211          fclose($fp);
 212          $getdata_error = "Fgets error. Did not receive any data back from Yahoo";
 213          return false;
 214      }
 215  
 216      // Check for error responses

 217      if (!(strstr($line, '200'))) {
 218  
 219          if (strstr($line, '403')) {
 220              fclose($fp);
 221              $getdata_error = "Yahoo HTTP 403 error. You do not have permission to access this resource, or are over your rate limit.";
 222              return false;
 223          }
 224  
 225          if (strstr($line, '503')) {
 226              fclose($fp);
 227              $getdata_error = "Yahoo HTTP 503 error. An internal problem prevented us from returning data to you.";
 228              return false;
 229          }
 230  
 231          if (strstr($line, '400')) {
 232              $errorflag = true;
 233              // error message will be returned in the response

 234          }
 235  
 236      }
 237  
 238      // Find blank line between header and data

 239      do {
 240          $line = fgets($fp , 1024);
 241          if ($line == false) {
 242              fclose($fp);
 243              $getdata_error = "Fgets: did not receive enough data back from Yahoo";
 244              return false;
 245          }
 246          if (strlen($line) < 3) {
 247              break;
 248          }
 249      } while (true);
 250  
 251      $xml='';
 252      // Fetch the data from Yahoo

 253      while ($line = fread($fp, 8192))
 254      {
 255          if ($line == false) {
 256              fclose($fp);
 257              $getdata_error = "Fread: error reading data from Yahoo";
 258              return false;
 259          }
 260          $xml .= $line;
 261      }
 262  
 263      fclose($fp);
 264  
 265      if ($errorflag) {
 266          $getdata_error = $xml;
 267          return false;
 268      } else {
 269          return $xml;
 270      }
 271  
 272  }
 273  
 274  
 275  /**

 276   * PostData -- Fetch a URL using an HTTP 1.0 POST operation

 277   *

 278   * PostData returns a string that is the result of doing

 279   * an HTTP 1.0 POST request on the URL. If an error occurs, false

 280   * is returned and the global $postdata_error is set to describe the error.

 281   *

 282   * @param string $url A valid HTTP URL

 283   * @param integer $timeout Number of seconds to block waiting for a response

 284   * @return boolean|string False or the result of posting the elements of the URL

 285   *

 286   */
 287  function PostData($url, $timeout) {
 288      global $postdata_error;
 289  
 290      // Parse the URL into parameters for fsockopen

 291      $UrlArr = parse_url($url);
 292      $host = $UrlArr['host'];
 293      $port = (isset($UrlArr['port'])) ? $UrlArr['port'] : 80;
 294      $path = $UrlArr['path'];
 295      $query = $UrlArr['query'];
 296  
 297      // Zero out the error response

 298      $errno = null;
 299      $errstr = '';
 300      $postdata_error = '';
 301      $errorflag = false;
 302  
 303      // Open the connection to Yahoo

 304      $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
 305  
 306      // Failed to open the URL

 307      if (!is_resource($fp)) {
 308          $postdata_error = "Fsockopen error number = $errno Details = $errstr";
 309          return false;
 310      }
 311  
 312      // Send HTTP headers

 313      if (!(fwrite($fp, 'POST '. $path .' HTTP/1.0' . "\r\n". 'Host: ' . $host . "\r\n" . 'Content-Length: '.strlen($query). "\r\n" . 'Content-Type: application/x-www-form-urlencoded'. "\r\n\r\n"))) {
 314          fclose($fp);
 315          $postdata_error = "Fwrite error. Could not write HTTP headers";
 316          return false;
 317      }
 318  
 319      // Write the query

 320      if (!(fwrite($fp, $query. "\r\n"))) {
 321          fclose($fp);
 322          $postdata_error = "Fwrite error. Could not write query";
 323          return false;
 324      }
 325  
 326      // Block on the socket port, waiting for response from Yahoo

 327      if (function_exists('socket_set_timeout')) {
 328          @socket_set_timeout($fp, $timeout);
 329          socket_set_blocking($fp, true);
 330      }
 331  
 332      // Get the HTTP response code from Yahoo

 333      $line = fgets($fp , 1024);
 334  
 335      if ($line == false){
 336          fclose($fp);
 337          $postdata_error = "Fgets error. Did not receive any data back from Yahoo";
 338          return false;
 339      }
 340  
 341      // Check for error responses

 342      if (!(strstr($line, '200'))) {
 343  
 344          if (strstr($line, '403')) {
 345              fclose($fp);
 346              $postdata_error = "Yahoo HTTP 403 error. You do not have permission to access this resource, or are over your rate limit.";
 347              return false;
 348          }
 349  
 350          if (strstr($line, '503')) {
 351              fclose($fp);
 352              $postdata_error = "Yahoo HTTP 503 error. An internal problem prevented us from returning data to you.";
 353              return false;
 354          }
 355  
 356          if (strstr($line, '400')) {
 357              $errorflag = true;
 358              // error message will be returned in the response

 359          }
 360  
 361      }
 362  
 363      // Find blank line between header and data

 364      do {
 365          $line = fgets($fp , 1024);
 366          if ($line == false) {
 367              fclose($fp);
 368              $postdata_error = "Fgets: did not receive enough data back from Yahoo.";
 369              return false;
 370          }
 371          if (strlen($line) < 3) {
 372              break;
 373          }
 374      } while (true);
 375  
 376      $xml='';
 377      // Fetch the data from Yahoo

 378      while ($line = fread($fp, 8192))
 379      {
 380          if ($line == false) {
 381              fclose($fp);
 382              $postdata_error = "Fread: error reading data from Yahoo";
 383              return false;
 384          }
 385          $xml .= $line;
 386      }
 387  
 388      fclose($fp);
 389  
 390      if ($errorflag) {
 391          $postdata_error = $xml;
 392          return false;
 393      } else {
 394          return $xml;
 395      }
 396  }
 397  
 398  
 399  
 400  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]