1

To my smartest friends, I googled and tried everything. I am sure that this is just the late night not letting me see/understand the problem. I should mention that I am still in the process of switching to OOP from functional, and I this is my first time I was able to have curl enabled. :( So, feel free to critique any bad practices also..

<?php
class urlValidation
{
    public $content;
    public $validUrl;
    public function __construct($validUrl)
    {
        $this->content = $content;
        $this->validUrl = $validUrl;
        $this->getContents();
    }
    public function getContents()
    {
        $this->curlHandler = curl_init($this->validUrl);
        curl_setopt($this->curlHandler, CURLOPT_NOBODY, false);
        curl_setopt(CURLOPT_RETURNTRANSFER, false); //changing false to true is the answer!
        $this->content = curl_exec($this->curlHandler);
        curl_close($this->curlHandler);
    }
}
?>

called from

$suggestUrl = 'http://www.google.com';
$validate = new urlValidation($suggestedUrl);

For all of the best of me, I cannot figure out why $this->content prints to the screen without being called. I just simply want that returned HTML to be stored in a variable and not returned/printed/appended to the document.

4
  • Show us how did you initialize your class and call its methods Commented Sep 6, 2012 at 6:35
  • Could you please specify what means "prints to screen without being called" - what does it print? As well as where have you instantiated the new object of that class. Also, you're setting $this->content=$content in the constructor, which doesn't make a lot of sense - where does $content come from? And lastly, it's a good practice to stick to conventions, so why not start a class's name with a capital letter. Commented Sep 6, 2012 at 6:36
  • @havelock I mean like reads the file's contents and appends it to the dom... it's as if an echo $this->content is happening but its NOT! Commented Sep 6, 2012 at 6:37
  • lol sorry for the pure code rage @havelock. As for your OOP critique, I'm not sure why the $this->content = $content is in the constructor. Those freaking mounds of books and tutorials always do it lol. I thought it was a local inheritance and scope thing, but maybe not. Commented Sep 6, 2012 at 6:50

1 Answer 1

3

PHP documentation explains it all ;)

curl_exec returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.

So before $this->content = curl_exec($this->curlHandler); you need to add this

curl_setopt($this->curlHandler, CURLOPT_RETURNTRANSFER, true);
Sign up to request clarification or add additional context in comments.

6 Comments

It still returns and prints the html to the second page without being called. :/ lol All I want is the returned html saved into a variable.
@JaredDrake All I want is the returned html saved into a variable. $this->content = file_get_contents('http://google.com'); will result the same?
@JaredDrake sorry, my bad, I forgot to add $this->curlHandler as first parameter for curl_setopt, and option should be true. See edited answer.
@Alexander Larikov I already adjusted that in for you when you gave me the original answer and it didn't work :/
@Alexander Larikov wait a minute... You're my freaking hero... Changing that to true did exactly what I wanted. GOD bless you my friend. Thank you so much. :)
|

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.