-2

I tried to use curl with if condition, It's not work.

if($_GET[test] != '')
{
    $fb = new FacebookDebugger();
    $fb->reload('http://www.example.com');

    class FacebookDebugger
    {
        public function reload($url)
        {
            $graph = 'https://graph.facebook.com/';
            $post = 'id='.urlencode($url).'&scrape=true';
            return $this->send_post($graph, $post);
        }
        private function send_post($url, $post)
        {
            $r = curl_init();
            curl_setopt($r, CURLOPT_URL, $url);
            curl_setopt($r, CURLOPT_POST, 1);
            curl_setopt($r, CURLOPT_POSTFIELDS, $post);
            curl_setopt($r, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($r, CURLOPT_CONNECTTIMEOUT, 5);
            $data = curl_exec($r);
            curl_close($r);
            return $data;
        }
    }
}

But when remove if condition, curl will be work good.

How can i do for using curl with if condition ?

1 Answers1

2

You are trying to call a function before initializing, try below code.

class FacebookDebugger
{
    public function reload($url)
    {
        $graph = 'https://graph.facebook.com/';
        $post = 'id='.urlencode($url).'&scrape=true';
        return $this->send_post($graph, $post);
    }

    private function send_post($url, $post)
    {
        $r = curl_init();
        curl_setopt($r, CURLOPT_URL, $url);
        curl_setopt($r, CURLOPT_POST, 1);
        curl_setopt($r, CURLOPT_POSTFIELDS, $post);
        curl_setopt($r, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($r, CURLOPT_CONNECTTIMEOUT, 5);
        $data = curl_exec($r);
        curl_close($r);
        return $data;
    }
}

if (isset($_GET[test]) && $_GET[test] != '') {
    $fb = new FacebookDebugger();
    $fb->reload('http://www.example.com');
}
  • OP: *"But when remove if condition, curl will be work good."* could also have something to do with `$_GET[test]` not being quoted. – Funk Forty Niner Oct 28 '15 at 17:58
  • Sidenote: `if (isset($_GET[test]) && $_GET[test] != '')` could be reduced to simply `if(!empty($_GET[test]))` – Funk Forty Niner Oct 28 '15 at 18:01
  • @Fred-ii- Always use isset to check the existence of a certain variable. Trying to access $_GET[test] will also raise an E_Notice when there is no "test" variable defined. Checking with isset can avoid this. – Devendra Singh Bhandari Oct 28 '15 at 18:14
  • I quote http://stackoverflow.com/a/7191642/ : *"Empty checks if the variable is set and if it is it checks it for null, "", 0, etc"*. – Funk Forty Niner Oct 28 '15 at 18:16
  • 1
    and from the manual http://php.net/manual/en/function.empty.php I quote: *"No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false."* - However, in some special instances, you could be right. – Funk Forty Niner Oct 28 '15 at 18:19
  • I was checking the same manual. You are right. – Devendra Singh Bhandari Oct 28 '15 at 18:27
  • Yeah, it's been debated before. However, and as I said earlier. There may be times where using both `isset()` and checking if an array equals something will be useful. I wasn't downgrading your answer ;-) *Cheers* – Funk Forty Niner Oct 28 '15 at 18:29
  • You're welcome Devendra, *cheers* – Funk Forty Niner Oct 28 '15 at 18:33