0

I'm trying to make an xmlhttp request and print out the xml. I created a class with methods and am calling those methods from an object. However, when I attempt to print the output of the method, I get nothing. I'm guessing it's something minor, but I've been trying for awhile now and have made little progress. Thanks in advance for the help.

<?php 
class twitter {

    public $screen_name;
    public $xml;
    public $count;

    public function getUserTimeline($screen_name, $count=5) {
        $request= "https://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name=$screen_name&count=$count";
        return $this->makeRequest($request);
    }

    public function makeRequest($request){
        return $xml = simplexml_load_file($request);

    }

}


$test = new twitter;
$test->screen_name="mattcutts";
$test->getUserTimeline($screen_name=$test->screen_name, $count=5);
print_r($test->xml); //This does not print anything.

?>

3 Answers 3

4

You're creating and returning a local variable $xml here in your makeRequest() method:

    return $xml = simplexml_load_file($request);

That should simply be $this->xml:

    $this->xml = simplexml_load_file($request);
Sign up to request clarification or add additional context in comments.

Comments

1

You try to access xml variable. But you didn't set it. You can change your method as following.

public function makeRequest($request){
    $this->xml = simplexml_load_file($request);
}

Or you can print $xml as following way.

$xmp = $test->getUserTimeline($screen_name=$test->screen_name, $count=5);
print_r($xml); 

Comments

-2

To create twitter object you must use constructor. So instead of $test = new twitter; use $test = new twitter();

1 Comment

$test = new twitter; is perfectly legal in PHP. The class doesn't have a constructor anyway, so whether you have the parentheses there or pass any arguments, there's no effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.