Part of the EllisLab Network
   
 
Need A Push In The Right Direction - Yahoo REST Web Services
Posted: 06 May 2007 06:33 PM   [ Ignore ]  
Summer Student
Total Posts:  24
Joined  04-14-2007

Greetings,

I want to play with some of Yahoo’s web services, such as their local web services:
http://developer.yahoo.com/search/local/V1/localSearch.html

...so i’m currently reading their “newbie” page where they tell you that you can communicate with them via file_get_contents or curl:

http://developer.yahoo.com/php/howto-reqRestPhp.html

My question is there anything (almost like a helper or a class) that I can use with Yahoo’s REST web services? Does anyone know of the best practice for working with something like this (the xml-rpc class would be great, but this is a different format i’m guessing, right?).

Anyways, have pity on a data feed newbie.

Thanks.

Profile
 
 
Posted: 07 May 2007 08:19 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  175
Joined  08-11-2006

There isn’t anything in the core of CI for that. It shouldn’t be hard to implement though.

The only web services libraries are the XML-RPC and XML-RPC Server libraries which in particular I don’t like very much but haven’t really used them on any serious projects.

I suppose CI should start focusing a little more on web services now than the rest of the framework features have really matured a lot since it’s first versions

 Signature 

Security Code Review

Profile
 
 
Posted: 07 May 2007 08:32 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  24
Joined  04-14-2007

That’s what i thought. I can’t find anything similar in the wiki either. I mean, it seems easy enough to do it outside of CI but I wanted the CI-style of doing things.

If this is truly the way i’m going, into unknown CI waters here, then which way would be best you think if i was trying to incorporate this in CI structure - curl or file_get_contents? Anyone have a preference?

Profile
 
 
Posted: 07 May 2007 09:07 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  8
Joined  05-03-2007

I have found curl to be one of the fastest methods.  You just need to install libcurl.

Here is a down and dirty curl class from

Keith Kurson

http://www.dreamincode.net/code/snippet632.htm

class curl {
  
/*
  * @author Keith Kurson (delusions@gmail.com)
  * @date September 09, 2006
  * @version 1.0
  */
     /*
     * Headers
     */
     
var $headers;
     
/*
     * User Agent
     */
     
var $user_agent;
     
/*
     * Compression
     */
     
var $compression;
     
/*
     * Cookie File
     */
     
var $cookie_file;
     
/*
     * Proxy Server
     * ip:port
     */
     
var $proxy;
     
/*
     * Initiate the class
     */
     
function cURL($cookies=TRUE,$cookie='cookies.txt',$compression='gzip',$proxy='') {
           $this
->headers[] = "Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg";
           
$this->headers[] = "Connection: Keep-Alive";
           
$this->headers[] = "Content-type: application/x-www-form-urlencoded";
           
$this->user_agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)";
           
$this->compression=$compression;
           
$this->proxy=$proxy;
           
$this->cookies=$cookies;
           
//if ($this->cookies == TRUE) $this->cookie($cookie);
     
}
     
/*
     * Tests the Cookie File
     */
     
function cookie($cookie_file) {
          
if (file_exists($cookie_file)) {
                $this
->cookie_file=$cookie_file;
          
} else {
                
@fopen($cookie_file,'w') or $this->error("The cookie file could not be opened. Make sure this directory has the correct permissions");
                
$this->cookie_file=$cookie_file;
                
fclose($cookie_file);
          
}
     }
     
/*
     * Runs a GET through cURL
     */
     
function get($url,$refer='') {
          $process
= curl_init($url);
          
curl_setopt($process, CURLOPT_REFERER, $refer);
          
curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
          
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
          if (
$this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
          if (
$this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
          
curl_setopt($process,CURLOPT_ENCODING , $this->compression);
          
curl_setopt($process, CURLOPT_TIMEOUT, 30);
          if (
$this->proxy) curl_setopt($cUrl, CURLOPT_PROXY, 'proxy_ip:proxy_port');
          
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
          
$return = curl_exec($process);
          
curl_close($process);
          return
$return;
     
}
     
/*
     * Runs a POST through cURL
     */
     
function post($url,$data,$refer) {
          $process
= curl_init($url);
          
curl_setopt($process, CURLOPT_REFERER, $refer);
          
curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
          
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
          if (
$this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
          if (
$this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
          if (
$this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIE, $this->cookies);
          
curl_setopt($process, CURLOPT_ENCODING , $this->compression);
          
curl_setopt($process, CURLOPT_TIMEOUT, 30);
          if (
$this->proxy) curl_setopt($cUrl, CURLOPT_PROXY, 'proxy_ip:proxy_port');
          
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
          
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
          
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
          
curl_setopt($process, CURLOPT_POST, 1);
          
$return = curl_exec($process);
          
curl_close($process);
          return
$return;
     
}
     
/*
     * Error Output
     */
     
function error($error) {
          
echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>";
          die;
     
}
}

Its easy to use.

Sample

$curlVars = array($cookies=FALSE,$cookie='',$compression='gzip',$proxy='');
      
$this->load->library('curl',$curlVars);

      
$postData = array("postfield_one"=>$postfield_one,"postfield_two"=>$postfield_two);
      
$url = "http://yoururl.com/controller/";
      
$refer ='';
        
      
$returnVar = $this->curl->post($url,$postData,$refer);
Profile
 
 
Posted: 07 May 2007 09:35 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  24
Joined  04-14-2007

First, i’m honored that you use your first post on me. grin

Secondly, wow. I got to try this out later today. Thanks for this… now, i assume curl isn’t a memory or resource hog? I plan on someday putting a large website together for a client that might want to use feeds and such like Yahoo…

Thanks again. If anyone has anymore feedback/thoughts, please… BRING IT ON!

Profile
 
 
Posted: 07 May 2007 10:06 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  8
Joined  05-03-2007

Curl is very lite resource wise and very fast. 

A helper (function set) that goes well with this is Keith Devans XML library. http://keithdevens.com/software/phpxml

Basically this provides array to xml conversion (XML_serialize) and xml to array conversion (XML_unserialize).

###################################################################################
#
# XML Library, by Keith Devens, version 1.2b
# http://keithdevens.com/software/phpxml
#
# This code is Open Source, released under terms similar to the Artistic License.
# Read the license at http://keithdevens.com/software/license
#
###################################################################################

###################################################################################
# XML_unserialize: takes raw XML as a parameter (a string)
# and returns an equivalent PHP data structure
###################################################################################
function XML_unserialize(&$xml){
    $xml_parser
= new XML();
    
$data = $xml_parser->parse($xml);
    
$xml_parser->destruct();
    return
$data;
}
###################################################################################
# XML_serialize: serializes any PHP data structure into XML
# Takes one parameter: the data to serialize. Must be an array.
###################################################################################
function XML_serialize(&$data, $level = 0, $prior_key = NULL){
    
if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
    reset
($data);
    while(list(
$key, $value) = each($data)){
        
if(!strpos($key, ' attr')){ #if it's not an attribute
            #we don't treat attributes by themselves, so for an empty element
            # that has attributes you still need to set the element to NULL

            
if(is_array($value) and array_key_exists(0, $value)){
                XML_serialize
($value, $level, $key);
            
}else{
                $tag
= $prior_key ? $prior_key : $key;
                echo
str_repeat("\t", $level),'<',$tag;
                if(
array_key_exists("$key attr", $data)){ #if there's an attribute for this element
                    
while(list($attr_name, $attr_value) = each($data["$key attr"]))
                        echo
' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
                    
reset($data["$key attr"]);
                
}

                
if(is_null($value)) echo " />\n";
                elseif(!
is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
                else echo
">\n", XML_serialize($value, $level+1), str_repeat("\t", $level), "</$tag>\n";
            
}
        }
    }
    reset
($data);
    if(
$level == 0){ $str = ob_get_contents(); ob_end_clean(); return $str; }
}
###################################################################################
# XML class: utility class to be used with PHP's XML handling functions
###################################################################################
class XML{
    
var $parser;   #a reference to the XML parser
    
var $document; #the entire XML structure built up so far
    
var $parent;   #a pointer to the current parent - the parent will be an array
    
var $stack;    #a stack of the most recent parent at each nesting level
    
var $last_opened_tag; #keeps track of the last tag opened.

    
function XML(){
         $this
->parser = xml_parser_create();
        
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
        
xml_set_object($this->parser, $this);
        
xml_set_element_handler($this->parser, 'open','close');
        
xml_set_character_data_handler($this->parser, 'data');
    
}
    
function destruct(){ xml_parser_free($this->parser); }
    
function parse(&$data){
        $this
->document = array();
        
$this->stack    = array();
        
$this->parent   = &$this->document;
        return
xml_parse($this->parser, $data, true) ? $this->document : NULL;
    
}
    
function open(&$parser, $tag, $attributes){
        $this
->data = ''; #stores temporary cdata
        
$this->last_opened_tag = $tag;
        if(
is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
            
if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
                #this is the third or later instance of $tag we've come across
                
$key = count_numeric_items($this->parent[$tag]);
            
}else{
                
#this is the second instance of $tag that we've seen. shift around
                
if(array_key_exists("$tag attr",$this->parent)){
                    $arr
= array('0 attr'=> &$this->parent["$tag attr"], &$this->parent[$tag]);
                    unset(
$this->parent["$tag attr"]);
                
}else{
                    $arr
= array(&$this->parent[$tag]);
                
}
                $this
->parent[$tag] = &$arr;
                
$key = 1;
            
}
            $this
->parent = &$this->parent[$tag];
        
}else{
            $key
= $tag;
        
}
        
if($attributes) $this->parent["$key attr"] = $attributes;
        
$this->parent  = &$this->parent[$key];
        
$this->stack[] = &$this->parent;
    
}
    
function data(&$parser, $data){
        
if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
            
$this->data .= $data;
    
}
    
function close(&$parser, $tag){
        
if($this->last_opened_tag == $tag){
            $this
->parent = $this->data;
            
$this->last_opened_tag = NULL;
        
}
        array_pop
($this->stack);
        if(
$this->stack) $this->parent = &$this->stack[count($this->stack)-1];
    
}
}
function count_numeric_items(&$array){
    
return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
}
Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120375 Total Logged-in Users: 19
Total Topics: 126496 Total Anonymous Users: 4
Total Replies: 665217 Total Guests: 284
Total Posts: 791713    
Members ( View Memberlist )