$request_url ="https://graph.facebook.com/".$uid."/apprequests?".$access_token;
$requests = file_get_contents($request_url);
This gets all the requests for a user. But how do I delete all of them at once? Facebook only has an example for deleting them one by one.
Thanks!
You can't delete multiple items in a single operation (like you can with, say, SQL). You will need to iterate to some degree to specify the unique URL for each request. What you can do is batch up your operations into a single request to Graph API.
More info here at FB.
I know this question is old, but I found it searching on Google so I thought anyone could need an answer now.
The best method for me, is to send all the request ids to Facebook on a single api call.
Your request ids must be on the format requestid_userid, like 12345_67890, supposing you have all the IDs inside an array ($array_of_request_ids), the code (PHP) would be like this:
$ids = implode(',', $array_of_request_ids);
$facebook->api("/?ids={$ids}", 'DELETE');
That should delete all the requests.
if($requests) {
foreach($requests as $key => $data) {
$request_url = "https://graph.facebook.com/" .
$data['id'] . "?" . $access_token;
$requests = file_get_contents($request_url);
//Delete a request.
$delete_url = $request_url . "&method=delete";
$result = file_get_contents($delete_url);
}
}
Related
I'm having problems passing the results of a friends/ids request to a users/lookup request while using Abraham's Twitter OAuth (https://twitteroauth.com/) PHP library to access the Twitter REST API.
After authenticating, I get an account's friends list as user ids:
$content = $connection->get("followers/ids", ["screen_name" => $input]);
Then I'm creating a comma separated list:
foreach ($content as $user) {
$userlist = implode(', ', $user);
And then I'm passing this to a users/lookup request:
$output = $connection->post("users/lookup", ["user_id" => $userlist]);
This gives a code 17 error which I understand to mean no such account was identified by Twitter. Outputting the imploded $userlist shows this step is working ok.
If I define $userlist myself then the subsequent call to users/lookup works fine. For example:
$userlist = "820310862045052930, 806614673474912256, 745020013837434880, 789205729123065860, 717272899741204480, 2523773164, 763810846929719296, 817061186705457152, 806495626670186496, 1935657786, 813858305282109442, 224295002, 24016369, 719472791200739328, 3292608016, 544394440, 338499233, 704776216, 1080910670, 2162932007, 15700673, 2212757984, 375238808, 2949937593, 244523746, 145021177, 4195801821, 799570638847561728"
I've tried converting the results of the first request (friends/ids) to array:
$contentarray = json_decode(json_encode($content), True);
but this makes no difference. I've also tried passing the list of ids as an array (and defining the $userlist as such in the request). Wrapping $userlist in quotes doesn't work either and it makes no difference if I use GET or POST. Similarly, creating another array of just user_ids (excluding the cursors) and creating a comma separated list from this does not make a difference.
Twitter OAuth is usually so simple and intuitive to use but I've spent hours on this and am getting nowhere. Can anyone help out with where I'm going wrong?
So it turns out the problem was with my foreach loop. Hat tip to hobbes3 for this answer.
After authenticating:
// get the user's followers
$content = $connection->get("followers/ids", ["screen_name" => $input]);
// replace stdclass object with array
$content_as_array = json_decode(json_encode($content), True);
//create new array with foreach
$content_as_new_array = array();
foreach($content_as_array as $user) {
foreach($user as $newuser) {
array_push($content_as_new_array, $newuser);
}
}
// implode this new array
$userlist = implode(", ", $content_as_new_array);
// and pass this new array to API
$output = $connection->post("users/lookup", ["user_id" => $userlist]);
Could anyone help me to create search for movies using PHP when connecting to the Rotten Tomatoes API?
On the Rotten Tomatoes site they give you example code how to get content for specific movie like so:
<?php
$apikey = 'insert_your_api_key_here';
$q = urlencode('Toy Story'); // make sure to url encode an query parameters
// construct the query with our apikey and the query we want to make
$endpoint = 'http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=' . $apikey . '&q=' . $q;
// setup curl to make a call to the endpoint
$session = curl_init($endpoint);
// indicates that we want the response back
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// exec curl and get the data back
$data = curl_exec($session);
// remember to close the curl session once we are finished retrieveing the data
curl_close($session);
// decode the json data to make it easier to parse the php
$search_results = json_decode($data);
if ($search_results === NULL) die('Error parsing json');
// play with the data!
$movies = $search_results->movies;
echo '<ul>';
foreach ($movies as $movie) {
echo '<li>' . $movie->title . " (" . $movie->year . ")</li>";
}
echo '</ul>';
?>
I'm an beginner with PHP so an example would be great. I have manage to solve this with JavaScript but the server I have to host the page won't display it because of updates. So now I will have to turn to PHP because it displays the code from above too.
You need to create a form with a text field. Name the field q so it will work with your sample code.
Have the form post to the url of your sample php script.
Remove this line:
$q = urlencode('Toy Story');
Replace it with this:
$q = urlencode($_POST['q']);
That should get you started.
Note that no effort is made to sanitize the $_GET. I am just providing the basics you need to have a search form running based on the example code you posted.
If you do not know how to create a form, here is a link to a tutorial:
http://www.tizag.com/phpT/forms.php
I've got a new SMS service for my php app. To send a message you need to go to an URL with the number and message as parameters.
This sms service sucks, is from an external company I know :(
//example.com/sms/?c=number&m=message
With an array of data I made a foreach loop to send each message.
<?php
// Users array
$users[] = ['Jhon', 123456789]
$users[] = ['Peter', 223456789]
$users[] = ['Sam', 323456789]
$users[] = ['Carl', 423456789]
// The loop
foreach ($users as $thisUser) {
// URL builded with the users data
$thisNumber = $thisUser[1];
$thisMessage = 'Hey,+your+name+is+' . $thisUser[0]
$request = sprintf('http://example.com/sms/?c=%d&m=Hey+%s+', $thisNumber, $thisMessage);
}
?>
Now, ¿witch is the best way to go that URL in a loop [several times]? Header redirection doesn't work for me. Maybe cURL...
I don't think JavaScript is a good solution. From the sound of it, you want to call that remote url so you can send an SMS. You can either use CURL or file_get_contents and just ignore the results (unless you need the results).
I would like to know how I can make AJAX calls to the Twitter API in order to get pictures of twitter users and display them back to the browser . The thing is that when I use the PHP method , it takes a VERY VERY LONG time to display profile pictures of a 100 twitter users generated by a loop .. So through this URL below that returns information about a twitter user, how could I use it with Jquery AJAX in order to fetch profile pictures images ? This requires the call to be authenticated ..
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
Below is the PHP method I used , but it's SOO SLOW ..
$id = $_GET['screen_name'];
* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
foreach($id as $key => $value) {
$url = $connection->get('users/show', array('screen_name' => $value));
$results = json_encode($url);
$data = json_decode($results, true);
$image = '';
if(is_array($data)){
$image = $data['profile_image_url']; ;
$image_bigger = str_replace('_normal', '_bigger',$image);
}
}
?>
Try the users/lookup endpoint which will let you pull 100 profiles at a time.
https://dev.twitter.com/docs/api/1.1/get/users/lookup
Note also that you cannot make pure ajax calls directly to the new Twitter API without exposing your secret keys to the public. You'll have to proxy these calls via your own back end.
I'm working on a small and simple code which basically does some tweets filtering. The problem is that I'm hitting the request limit of Twitter API and I would like to know if there is a workaround or if what I want to do just cannot be done.
First, I type a twitter username to retrieve the ID's of people this user follows.
$user_id = $_GET["username"];
$url_post = "http://api.twitter.com/1/friends/ids.json?cursor=-1&screen_name=" . urlencode($user_id);
$following = file_get_contents($url_post, true);
$json = json_decode($following);
$ids = $json->ids;
Twitter API responds with a list of ID's.
Here comes the problem. The next step is to make a request to find out username, profile picture and description for each one of those ID's.
$following = array();
foreach ($ids as $value)
{
$build_url = 'http://api.twitter.com/1/users/lookup.json?user_id=' . $value . '';
$following[] = $build_url;
}
foreach ($following as $url)
{
$data_names = file_get_contents($url, true); //getting the file content
$json_names = json_decode($data_names);
foreach ($json_names as $tweet) {
$name = $tweet->name;
$description = $tweet->description;
echo '<p>';
echo $name . '<br>';
echo $description;
echo '</p>';
}
}
If the user follows 50 people it works. But if he follows, let's say, 600 hundred, that would be 600 hundred request (for username, description and profile pic) to Twitter API which exceeds the limit.
Is there any way to workaround this o it just cannot be done?
Thank you!
You can and should request users/lookup API endPoint with 100 userIds at a time, instead of doing one request per twitter ID. cf. https://dev.twitter.com/docs/api/1.1/get/users/lookup
You have to replace your forEach loop (foreach ($following as $url)) by a recursive function.
At the end of the function, check the number of hits remaining before calling it again (cf. this link to see how to know the time remining until you get rate limited).
If there is no hit left, sleep 15 minutes before calling the function again, otherwise do the call again.
There is plenty of information on how to do this, use Google and search existing stackOverflow questions.