I'm trying to send our users notifications via our Facebook app.
Probably to the tune of 50-100 at a time, and can't seem to get the cURL to loop through...I can get one to send, but that's it. Sure I'm just missing something simple, but would love some other sets of eyes on it!
The code is:
<?php
include('../inc.php');
//App Access Token
$appat = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=xxxxxxxxxx&client_secret=xxxxxxxxxxx&grant_type=client_credentials");
parse_str($appat);
//Notify
$sql = "SELECT * FROM ats";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$userid = $row['UserID'];
$url = "https://graph.facebook.com/$userid/notifications";
$attachment = array(
'access_token' => "$access_token",
'href' => "?a=1",
'template' => "Template Message",
'ref' => "ActT"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
//curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close ($ch);
}
include('../inc2.php');
?>
Thanks for the responses guys! The Facebook SDK wasn't an option unfortunately, and after some research, this solved my problem:
<?php
include('../inc.php');
$mh = curl_multi_init();
$appat = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=xxxxxxxxxx&client_secret=xxxxxxxx&grant_type=client_credentials");
parse_str($appat);
$sql = "SELECT * FROM ats";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$i = $row['ID'];
echo $i;
$userid = $row['UserID'];
$url = "https://graph.facebook.com/$userid/notifications";
$attachment = array( 'access_token' => "$access_token",
'href' => "?a=1",
'template' => "Template Message",
'ref' => "DanceCouple"
);
${'ch_' . $i} = curl_init();
curl_setopt(${'ch_' . $i}, CURLOPT_URL,$url);
curl_setopt(${'ch_' . $i}, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt(${'ch_' . $i}, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt(${'ch_' . $i}, CURLOPT_POST, true);
curl_setopt(${'ch_' . $i}, CURLOPT_POSTFIELDS, $attachment);
// build the multi-curl handle, adding both $ch
curl_multi_add_handle($mh, ${'ch_'.$i});
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
include('../inc2.php');
?>