Welcome Guest, Not a member yet? Register   Sign In
How to load a 3rd party library -> scope problems??
#1

[eluser]andjules[/eluser]
I'm having problems with the PHP library for the scribd api (follow the client libraries link).

When I run commands against the library OUTSIDE of CI, it performs fine. When I try to incorporate it inside CI and call it from a controller, I get undefined variable and undefined index PHP errors (it's definitely finding the library, instantiating the class, etc. as the errors are dealing with the XML response from Scribd). I'm not sure how the scope changes inside CI, so I was wondering if anyone might be able to explain that to my not-so-smart brain ;-)

[Interestingly, I was able to include some other Zend Framework libraries with no problems, using the 'lazy' method (create a helper that simply defines the base path, load the helper, then do a require once of the file)]

my controller code looks something like:
Code:
function scribd() {
            $this->load->helper('scribd');
            require_once "Scribd/scribd.php";
            // also tried: load_class('Scribd', FALSE);
            $scribd_api_key = "XXXYYYYXXXXYYY"; //add your own!
            $scribd_secret = "sec-123456789uvwxyz"; //add your own!
            $scribd = new Scribd($scribd_api_key, $scribd_secret);
            $newdoc = $scribd->getList();
            print_r($newdoc);
    }

the part of the Scribd library that deals with the response looks like this (note the author's comment... something's not quite right):
Code:
if($result['stat'] == "ok"){
                
                //This is shifty. Works currently though.
                $result = $this->convert_simplexml_to_array($result);
                if(urlencode((string)$result) == "

" && $this->error == 0){
                    $result = "1";
                    return $result;
                }else{
                    return $result;
                }
            }

and the convert_simplexml_to_array function from the Scribd library (the PHP errors trace to the "if($arr[$k]){" line):
Code:
public static function convert_simplexml_to_array($sxml) {
        $arr = array();
        if ($sxml) {
          foreach ($sxml as $k => $v) {
                if($arr[$k]){
                    $arr[$k." ".(count($arr) + 1)] = self::convert_simplexml_to_array($v);
                }else{
                    $arr[$k] = self::convert_simplexml_to_array($v);
                }
            }
        }
        if (sizeof($arr) > 0) {
          return $arr;
        } else {
          return (string)$sxml;
        }
    }
Thanks is advance for considering.
#2

[eluser]Chris Newton[/eluser]
That script must have errors suppressed somewhere, or whatever normally calling it does, because that's definitely an error, and the error doesn't lie with CI or a scope change.

You should change

Code:
if ($arr[$k])

to

Code:
if (isset($arr[$k]))
or maybe
Code:
if(array_key_exists($k,$arr))


That should get rid of the undefined index error.... the script you have above assumes that $arr has an array index of $k, when $arr was just created a few lines above with no index in the name of $k... so, yeah, the index is undefined, at least on the first pass.
#3

[eluser]andjules[/eluser]
thanks so much
that took me to another error, which I was able to fix up
then it worked!

then I went back to Zend Framework and realized their rest -> client library is really easy to understand, so I loaded that (in to CI) instead of this not-so-clean scribd-specific library, and it's even smoother.

using Zend Framework libraries in CI is turning out to be one of my favorite lazy-code-shortuts.

Anyhow, thanks for the pointer/fresh eyes. I officially owe you a pint ;-)




Theme © iAndrew 2016 - Forum software by © MyBB