Creating and inviting a group to an event via graph api - php

Is there still no way to invite all the group members to an group event? I am able to create the group event via the api, but members are not invited to the new event. If i create the event via the facebook website i have the option to select "invite all group members". I can't seem to find any way to reproduce that functionality via the api.

Since there was no way to do this directly via the graph api i ended up making my own php w/mysql solution. Basically i make a table of all my group's members, then i check if they have been invited to all my group's events. the good thing about this forced method is that if a new members joins after an event is created they will be invited to all future events.
facebook does not tell you how many members you can invite from the api per time period and not get flagged for spam
code to dump group members:
function UpdateFacebookMemberDB($verbatim = FALSE)
{
global $access_token, $groupID;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/members?access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decoded = json_decode($return, true);
$decoded = $decoded['data'];
Sql_Connect();
if(!Sql_Query("DELETE FROM `Facebook_User`;"))
{
echo "Could not empty the `Facebook_User` table.<br>\n";
return FALSE;
}
foreach ($decoded as $value)
{
$query="INSERT INTO `Facebook_User` (`Name`, `FID`) VALUES ('".Sql_CleanInput($value['name'])."', '".Sql_CleanInput($value['id'])."');";
if(Sql_Query($query))
{
if($verbatim)
echo $value['name']." was added to the database.<br>";
}
else if($verbatim)
echo $value['name']." was <b>NOT</b> added to the database.<br>";
}
if(!Sql_Query("DELETE FROM `Facebook_Event`;"))
{
echo "Could not empty the `Facebook_Event` table.<br>\n";
return FALSE;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/events?fields=start_time&since=now&access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decoded = json_decode($return, true);
$decoded = $decoded['data'];
foreach ($decoded as $value)
{
$eid = $value['id'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$eid.'/invited?access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decod = json_decode($return, true);
$decod = $decod['data'];
foreach ($decod as $val)
{
$query="INSERT INTO `Facebook_Event` (`EventID`, `UserID`) VALUES ('".$eid."', '".$val['id']."');";
if(Sql_Query($query))
{
if($verbatim)
echo $val['name']." was added to the database.<br>";
}
else if($verbatim)
echo $val['name']." was <b>NOT</b> added to the database.<br>";
}
}
Sql_Disconnect();
}
code to check/invite members:
function InviteClubToEvent)
{
global $access_token, $groupID;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/events?fields=start_time&since=now&access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decoded = json_decode($return, true);
$decoded = $decoded['data'];
$invite=0;
for($x=count($decoded)-1;$x>=0;$x--)
{
$eventid = $decoded[$x]['id'];
Sql_Connect();
$query="SELECT * from `Facebook_User` ORDER BY `FID` ASC;";
$qresult=Sql_Query($query);
$data = array( 'access_token' => $access_token);
$ch = curl_init();
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, $data);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
for($i=0;$i<Sql_Num_Rows($qresult);$i++)
{
$subquery="SELECT * from Facebook_Event WHERE `UserID` ='".Sql_Result($qresult,$i,"FID")."' AND `EventID` ='".$eventid."'LIMIT 1;";
if(Sql_Num_Rows(Sql_Query($subquery))==0)
{
$invite++;
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$eventid.'/invited/'.Sql_Result($qresult,$i,"FID"));
$return = curl_exec($ch);
if($return == "true")
{
echo "$invite) ".Sql_Result($qresult,$i,"Name") . " has been invited to $eventid!<br>";
Sql_Query("INSERT INTO `sdbmwcca_main`.`Facebook_Event` (`EventID`, `UserID`) VALUES ('".$eventid."', '".Sql_Result($qresult,$i,"FID")."');");
} else
{
echo "<hr><b>$invite) ".Sql_Result($qresult,$i,"Name") . " was not invited because: " . $return . "</b><hr>";
curl_close($ch);
Sql_Disconnect();
die();
}
}
if($invite > ##some spam limit##)
{
$x = 0;
break;
}
}
curl_close ($ch);
Sql_Disconnect();
}
}

Related

Terminate function a() when function b() failed

We are a little bit confuse how we can achieve this problem. We don't want to run the createsite function in our code if createSubaccount function fails. We would truly appreciate any feedbacks, comments, guides on our code.
<?php
//Set API user and password
define("API_USER","user");
define("API_PASS","pw");
$createdSite = createSite($_REQUEST['template_id'],$_REQUEST['original_url']);
//echo 'Site Created: ' . $createdSite . '<br/>';
$accountCreated = createSubAccount($_REQUEST['email']);//client email
//echo 'Account created: ' . $accountCreated . '<br/>';
$first_name = $_REQUEST['first_name'];//First Name
$last_name = $_REQUEST['last_name'];//Last Name
$retArr = ["sso"=>$sso_link,"ru"=>$resetURL,"ac"=>$accountCreated,"fn"=>$first_name,"ln"=>$last_name];//assoc array
print json_encode ($retArr);//json string
function createSite($template_id,$original_url) {
//create array with data
if($original_url) {
$data = array("template_id"=>$_REQUEST['template_id'],"url"=>$original_url);
} else {
$data = array("template_id"=>$_REQUEST['template_id']);
}
//turn data into json to pass via cURL
$data = json_encode($data);
//Set cURL parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.website.com/api/create');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//execute cURL call and get template data
$output = curl_exec($ch);
//check for errors in cURL
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
$output = json_decode($output);
return $output->site_name;//Output /Return : {"site_name":"28e1182c"}
}
function createSubAccount($emailToCreate) {
$first_name = $_REQUEST['first_name'];//First Name
$last_name = $_REQUEST['last_name'];//Last Name
$data = '{"account_name":"'.$emailToCreate.'", "first_name":"'.$first_name.'", "last_name":"'.$last_name.'"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.website.com/api/create');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//execute cURL call and get template data
$output = curl_exec($ch);
if(curl_getinfo($ch,CURLINFO_HTTP_CODE) == 204) {
curl_close($ch);
return $emailToCreate;//Expected return HTTP Code: 204 No Content
} else {
curl_close($ch);
$output = 'failed';
return $output;
die('Account creation failed, error: '. $output . '<br/>');
}
}
?>
This is where an Exception comes in handy.
function createSite() {
throw new \Exception('Failed');
}
try {
$createdSite = createSite($_REQUEST['template_id'],$_REQUEST['original_url']);
//echo 'Site Created: ' . $createdSite . '<br/>';
$accountCreated = createSubAccount($_REQUEST['email']);//client email
} catch(\Exception $err) {
echo $err->getMessage();
}
The Exception prevents the rest of the code in the block from executing once thrown.

Curl not giving any response back

I am using curl get the response back from a file on different server..
Curl code:
$url="http://example.com/trck.php?adrs=yy";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
trck.php Code:
$url=mysqli_real_escape_string($link,$_GET['adrs']);
$sql="SELECT * FROM product where site = '$url'";
$res=mysqli_query($link,$sql) or die( mysqli_error() );
$row=mysqli_fetch_assoc($res);
if(mysqli_num_rows($res)==0 || $row['status']==0)
{
return no;
}
else
{
return yes;
}
but there is no response form it.
Help will be appreciated.
$url=mysqli_real_escape_string($link,$_GET['adrs']);
$sql="SELECT * FROM product where site = '$url'";
$res=mysqli_query($link,$sql) or die( mysqli_error() );
$row=mysqli_fetch_assoc($res);
if(mysqli_num_rows($res)==0 || $row['status']==0)
{
echo 'no';
}
else
{
echo 'yes';
}
also:
$url="http://example.com/trck.php?adrs=yy";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
you have to echo the returned transfer from curl.

Echo avatars of project members Basecamp (PHP)

I'm working on a Basecamp widget which will have to show important information about the projects we are currently working on. Since I could not find any relevant and detailed documentation on this matter I would like to ask the community for help.
My Situation
I'm currently already echo-ing my projects, this data contains:
Project Name
Description
Project ID
And for my To-Do Items I echo the following data:
Remaining To-Do Items
Completed To-Do Items
Total To-Do Items
Progression in percentages
My goal
I'd like to show the avatars of all the people working on each individual project. Since this kind of programming is fairly new to me I'd appreciate some guidance on this matter.
Since Basecamp is becoming more popular I think more people could be interested in this information. I have consulted the API for Basecamp and written a small part to attempt the echo-ing of the project members but in vain.
I thank you for your help in advance.
Cheers,
J.I.N.
(Post scriptum: This is the complete source I have until now, some value's are deliberately altered to cloak data like credentials etc. Sorry for the long post, I tried to keep it as brief as possible)
Authentication & Retrieving Project Data
$appName = 'MyAppName';
$appContact = 'MyAddress';
$basecampAccountId = 'MyAccountID';
$basecampUsername = 'MyBaseCampUsername';
$basecampPassword = 'MyPassword';
$baseUrl = "https://basecamp.com/************/api/v1";
$url= $baseUrl.'/projects.json';
$credentials = "$basecampUsername:$basecampPassword";
$helloHeader = "User-Agent: $appName ($appContact)";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //optional
curl_setopt($ch, CURLOPT_MAXREDIRS, 3); //optional
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); //optional
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //optional
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); //optional
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($helloHeader));
$response = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
Retrieving To-Do Lists
$json = $response;
$data = json_decode($json);
/*echo "<br/><br/>";*/
foreach($data as $key)
{
echo "<ul>";
echo "<li>".$key->name."</li>";
echo "<li>".$key->id."</li>";
echo "<li>".$key->description."</li>";
$url= $baseUrl.'/projects/'.$key->id.'/todolists.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
// curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //optional
curl_setopt($ch, CURLOPT_MAXREDIRS, 3); //optional
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); //optional
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //optional
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); //optional
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($helloHeader));
$response = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
// print_r($response);
$json = $response;
$data = json_decode($json);
$todo_remain = $data[0]->remaining_count;
$todo_completed = $data[0]->completed_count;
$todo_total = $todo_remain + $todo_completed;
echo "<BR>";
echo "<BR>";
echo "Remaining To-Do Items:";
echo "<BR>";
echo $todo_remain;
echo "<BR>";
echo "Completed To-Do Items:";
echo "<BR>";
echo $todo_completed;
echo "<BR>";
echo "Total To-Do Items:";
echo "<BR>";
echo $todo_total;
echo "<BR>";
echo "Remaining percentage is: <BR>";
echo round(($todo_remain/$todo_total)*100);
echo "%";
echo "<BR>";
echo "Completed percentage is: <BR>";
echo round(($todo_completed/$todo_total)*100);
echo "%";
echo "</ul>";

how can i use a foreach on a foreach php

hi guys i have this script but obviously im not using foreach right i was wondering is there anyway i could combine these two requests to works as one
$url = "http://api.website.com/1.0/country?source=ballsche&programid=5380&campaignid=100000";
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$array = json_decode($rawdata,true);
foreach($array as $obj) {
$country = $obj['country'];
$countrycode = $obj['countryCode'];
}
foreach($countrycode as $did) {
$wgurl = "http://api.website.com/1.0/city?source=ballsche&programid=5380&campaignid=100000&country-code=$did";
$wgch = curl_init();
$wgtimeout = 0;
curl_setopt($wgch, CURLOPT_URL, $wgurl);
curl_setopt($wgch, CURLOPT_HEADER, 0);
curl_setopt($wgch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($wgch, CURLOPT_CONNECTTIMEOUT, $wgtimeout);
$wgrawdata = curl_exec($wgch);
curl_close($wgch);
$wgarray = json_decode($wgrawdata,true);
}
foreach($wgarray as $wgobj) {
$city = $wgobj['city'];
$citycode = $wgobj['cityCode'];
if($city){
$sql = "INSERT INTO `pt_city` (city, citycode) VALUES ('$city', '$citycode')";
database_queryModify($sql,$result);
}else{
echo "dint work";
}
}
there must be an easy way to do this im guesing creating another array from the data but i cant quite get it right i keep getting errors ive tried this and a few other things my problem is i need to cycle trough county codes and make requests from the code there a 150 requests i need to cycle trough to get all the city information and for each request i need to decode the json thats coming back and insert it into my city table
just put foreach inside each other like this
$url = "http://api.website.com/1.0/country?source=ballsche&programid=5380&campaignid=100000";
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$array = json_decode($rawdata,true);
foreach($array as $obj) {
$country = $obj['country'];
$countrycode = $obj['countryCode'];
foreach($countrycode as $did) {
$wgurl = "http://api.website.com/1.0/city?source=ballsche&programid=5380&campaignid=100000&country-code=$did";
$wgch = curl_init();
$wgtimeout = 0;
curl_setopt($wgch, CURLOPT_URL, $wgurl);
curl_setopt($wgch, CURLOPT_HEADER, 0);
curl_setopt($wgch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($wgch, CURLOPT_CONNECTTIMEOUT, $wgtimeout);
$wgrawdata = curl_exec($wgch);
curl_close($wgch);
$wgarray = json_decode($wgrawdata,true);
foreach($wgarray as $wgobj) {
$city = $wgobj['city'];
$citycode = $wgobj['cityCode'];
if($city){
$sql = "INSERT INTO `pt_city` (city, citycode) VALUES ('$city', '$citycode')";
database_queryModify($sql,$result);
} else {
echo "dint work";
}
}
}
}

Retrieve Android Market mylibrary with curl

I am trying to retrieve this page using curl in php. This page of course requires you to log in because it displays different apps for each user. I have been following the work done on this page, however am not having much success.
So far, in his example I am able to successfully populate the auth variable with the auth token. In the next step however (Below the comment for logging into Android Market) I run into troubles. The output variable that he says should have a 302 code results in a "The document has moved" page which links me back to the Google log in page.
Here is a pastebin to show exactly what I am trying. http://pastebin.com/9Fs9GWxk
Additionally if anyone knows what steps I need to do after this to actually get the page I need that would be amazing. Thanks
Here is something I came up with today for this question that has been modified to work for you:
<?php
$USERNAME = 'you#gmail';
$PASSWORD = 'yourpasswd';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Ubuntu; X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_URL,
'https://accounts.google.com/ServiceLogin?hl=en&continue=https://market.android.com/mylibrary');
$data = curl_exec($ch);
$formFields = getFormFields($data);
$formFields['Email'] = $USERNAME;
$formFields['Passwd'] = $PASSWORD;
unset($formFields['PersistentCookie']);
// var_dump($formFields);
$post_string = '';
foreach($formFields as $key => $value) {
$post_string .= $key . '=' . urlencode($value) . '&';
}
$post_string = substr($post_string, 0, -1);
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($ch);
//var_dump($result);
if (preg_match('/^2\d{2}/', curl_getinfo($ch, CURLINFO_HTTP_CODE)) == false) {
die("Login failed");
var_dump(curl_getinfo($ch), $result);
} else {
curl_setopt($ch, CURLOPT_URL, 'https://market.android.com/mylibrary');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$result = curl_exec($ch);
echo $result;
}
function getFormFields($data)
{
if (preg_match('/(<form id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
$inputs = getInputs($matches[1]);
return $inputs;
} else {
die('didnt find login form');
}
}
function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}

Categories