我尝试使用curl if if condition,它不起作用。
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;
}
}
}
但是当删除条件时,curl将会正常工作。
如何在条件下使用curl?
答案 0 :(得分:2)
您正在尝试在初始化之前调用函数,请尝试下面的代码。
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');
}