i am try to use try() inside foreach in php for facebook application my app get user permissions than post on his 3 friends wall but the problem is that if one of user friends is non-postable than my app stop with error below is my code and error please take a look
foreach ($friends_list_array["data"] as $value) {
try
{
// compile the post for for user
$WallPost = array(
'access_token' => $atoken,
'message' => $value["name"] . ' .. message here ',
'link' => 'link_here');
// post to user wall
$response = $facebook->api('/' . $value["id"] . '/feed','POST',$WallPost);
echo ' posted on ' . $value["name"];
}
}
all works fine(except non-postable wall problem) without try() its shows error below is error
Parse error: syntax error, unexpected '}', expecting T_CATCH in
Can any one please solve this thank you
Look here
Each try must have at least one corresponding catch block
And thats the answer to your problem ( the error itself says about it, expecting T_CATCH)
If you do not need catch, just don't fill it with any logic, working example:
foreach ($friends_list_array["data"] as $value) {
try{
// do stuff...
}
catch(Exception $e){
// do nothing
}
}
Related
I am trying to loop through an array of four servers. I am trying to find a user on those servers. I am able to see the program works but PHP throws errors from the servers it cannot find from the array. Could someone help how to avoid those errors?
$ID = '1234';
$server_list = array("example1.com", "example2.com", "example3.com");
function serverQuery($server_array, $ID){
foreach ($server_array as $value) {
$host = $value;
try{
include('info.php');
$response = $client->getUser(array("name" => ID))->return;
//print_r($response);
}catch (Exception $e){
echo "Print the error";
}
}
}
Every time I search the user, it did fine in one of the servers but "Print the Error" is also printed along with it. Please help.
Thanks.
I want to post an image on twitter but I can't. I am able to post only text message with twitter API.
The error is: Error: Server error: couldn't open file "Capture.png"
I use wamp server. The picture is in the same folder with the script.
Where is the problem?
require_once '../src/twitter.class.php';
// ENTER HERE YOUR CREDENTIALS (see readme.txt)
$twitter = new Twitter. ('raQBedybFLb', 'HWgN6Qt11jg0LY', '862846056-ndeGfMA83r9ldh', 'VMBvHDahXnGp');
$p='./Capture.png';
try {
$tweet = $twitter->send('testing twitter api', $p); // you can add $imagePath as second argument
} catch (TwitterException $e) {
echo 'Error: ' . $e->getMessage();
}
You may use $p = realpath('Capture.png');
i have a display that shows 6 random friends on facebook when using the app, however, what i would like to figure out is how to change the display, so that when the friend is clicked on, they are sent an invite to use the app.
Nothing flash, just some kind of notification to say something along the lines of 'Bob thinks you should use this app' and when clicked, they go to the app.
not sure if it will help, but here is the code i have to display friends of the user.
code:
<div class= "newboxbottom">
<h1>Why not share?</h1>
<?php
$user = $facebook->getuser();
if ($user) {
$user_profile = $facebook->api('/me');
$friends = $facebook->api('/me/friends?fields=first_name');
echo '<table>';
foreach (array_slice($friends["data"], 0, 6) as $value) {
echo '<td>';
echo '<div class="pic">';
echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture?type=normal"/>';
echo '</div>';
echo '<div align="center">','<font color="white">','<div class="picName">'.$value["first_name"].'</div>','</font>','</div>';
echo '</td>';
}
echo '</table>';
}
?>
</div>
many thanks to anyone who can help.
Try looking into the Notification API that Anvesh mentioned in a comment.
You'll want to be careful with notifications and follow the recommended best practices. You don't want to send notifications to users who would not want them in the first place. You could get hit with negative feedback and get shut down by Facebook.
As for how to actually make the call? I haven't actually used this particular API method, but from what I can gather, you should be able to do something like this:
<?php
$parameters = array(
'href' => ''
'template' => ''
'ref' => ''
);
try {
$response = $facebook->api('<enter recipient user facebook id here>/notifications', 'post', $parameters);
} catch (FacebookAPIException $e) {
$echo $e->getMessage();
}
Checkout the documentation on more information on the parameters you need to pass. Hope this helps.
I'm creating a module for joomla that lists the the contents of a given folder. This module works as a service, so no OAuth should be required.
I'm stuck since every request I make has the same answer, no items. So I would like to know if I'm creating the client/service in a wrong way or I may have registered the application in a wrong way.
Here is the code:
// this class is where I have all the IDs from the google console
require_once(dirname(__FILE__) . '/gdrive/config.php');
require_once(dirname(__FILE__) . '/gdrive/gd-v2-php/apiClient.php');
require_once(dirname(__FILE__) . '/gdrive/gd-v2-php/contrib/apiDriveService.php');
$client = new apiClient();
$key = file_get_contents(Config::KEY_FILE);
$client->setClientId(Config::CLIENT_ID);
$client->setUseObjects(true);
$client->setAssertionCredentials(new apiAssertionCredentials(
Config::SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/drive'), $key)
);
$service = new apiDriveService($client);
try {
$parameters = array();
$parameters['maxResults'] = $maxResults;
$files = $service->files->listFiles($parameters);
$items = $files->getItems();
var_dump($items);
$pageToken = $files->getNextPageToken();
} catch (apiServiceException $e) {
print "Error code :" . $e->getCode() . "\n";
// Error message is formatted as "Error calling <REQUEST METHOD> <REQUEST URL>: (<CODE>) <MESSAGE OR REASON>".
print "Error message: " . $e->getMessage() . "\n";
} catch (apiException $e) {
// Other error.
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";
} catch (Exception $e) {
print "Cannot retrieve the files";
print($e->getMessage());
}
Sorry to ask such a noob question but this is driving me crazy
Thanks
P.S. just for the record, I followed this google-api-php-client/wiki/OAuth2#Service_Accounts
After having a second look to the documentation I found that Google Drive does not support service accounts. I don't know if this is going to change but, with the requirements I have (I need to show in the web is what an end user uploads to a folder (which is public) using the desktop app) I think I cannot use google drive as a valid solution.
I found a solution but I don't like to much since I should have to refresh the token forever Google Drive SDK Service Account Error
What is meant by the below error?Why am i getting this.
Fatal error: Uncaught Exception: 601: Parser error: unexpected end of query. thrown in base_facebook.php on line 1039
I'm using a code to delete the invite once the user clicks on it from app tab,but this is not deleting the app request
Here is the code,
foreach ($request_ids as $request_id)
{
echo ("reqeust_id=".$request_id."<br>");
$full_request_id = build_full_request_id($request_id, $user_id);
echo ("full_request_id=".$full_request_id."<br>");
try {
$delete_success = $facebook->api("/$full_request_id",'DELETE');
if ($delete_success) {
echo "Successfully deleted " . $full_request_id;}
else {
echo "Delete failed".$full_request_id;}
}
catch (FacebookApiException $e) {
echo "error";}
}
Why is this bit in quotes?
$facebook->api("/$full_request_id",'DELETE');
Surely it should just be:
$facebook->api($full_request_id,'DELETE');
Other than that I see nothing wrong with your code. Although, it would be good to know what line 1039 in base_facebook.php is.