i am trying to update Facebook status using Facebook PHP SDK.
i have updated my status several time using the code below:
try {
$session = new FacebookSession($page_token);
$session->validate();
(new FacebookRequest($session, 'POST', '/' . $id . '/feed', array(
'message' => $message
)))->execute();
} catch(FacebookRequestException $e) {
.....
}
but it giving me error while i am trying to update this status:
RiversofGrue: Returns To The Palace http://clc.li/cxG #Grueheads #horror #crashpalace
surprisingly a have updated this status from my localhost successfully. but in the production server it is throwing error.
The error is:
Error Message: couldn't open file "RiversofGrue: Returns To The Palace http://clc.li/cxG #Grueheads #horror #crashpalace"
File: .../vendor/facebook/php-sdk-v4/src/Facebook/HttpClients/FacebookCurlHttpClient.php
Error Code: 26
Line: 150
TraceAsString: #0 .../vendor/facebook/php-sdk-v4/src/Facebook/FacebookRequest.php(248): Facebook\HttpClients\FacebookCurlHttpClient->send()
#1 .../Demo.php(146): Facebook\FacebookRequest->execute()
#2 .../DemoDemo.php(31): Demo->execute()
#3 .../vendor/laravel/framework/src/Illuminate/Console/Command.php(112): FacebookJobCommand->fire()
#4 .../vendor/symfony/console/Symfony/Component/Console/Command/Command.php(253): Illuminate\Console\Command->execute()
#5 .../vendor/laravel/framework/src/Illuminate/Console/Command.php(100): Symfony\Component\Console\Command\Command->run()
#6 .../vendor/symfony/console/Symfony/Component/Console/Application.php(889): Illuminate\Console\Command->run()
#7 .../vendor/symfony/console/Symfony/Component/Console/Application.php(193): Symfony\Component\Console\Application->doRunCommand()
#8 .../vendor/symfony/console/Symfony/Component/Console/Application.php(124): Symfony\Component\Console\Application->doRun()
#9 .../artisan(59): Symfony\Component\Console\Application->run()
#10 {main}
I don't know what is the problem. Any idea guys???
Thanks
Edit:
#ceejayoz 's answer help me a lot. i was working on the wrong path. but the main problem was #. if ceejayoz did not show me that, may be i was going to spend many more time to figure it out. if there was a way to accept two answer, i would love to accept ceejayoz also. thanks.
Original:
Finally i have decided to cut off the starting # from the string. so i have used this:
if(strpos($message, "#") === 0){
$message = substr($message, 1);
}
I did not find the solution, so i have used this path. if you have any other solution please post.
Your issue is that the message starts with #. cURL treats any parameter starting with # in a POST request to be a filename, and attempts to load and send that data's file contents as that parameter. This is very useful for things like file uploads, but a problem in your situation.
You should be able to fix this by wrapping your FacebookRequest parameters with http_build_query like so:
try {
$session = new FacebookSession($page_token);
$session->validate();
(new FacebookRequest($session, 'POST', '/' . $id . '/feed', http_build_query(array(
'message' => $message
))))->execute();
} catch(FacebookRequestException $e) {
.....
}
Related
I started with facebook business sdk for php. Was following this doc: https://developers.facebook.com/docs/business-sdk/getting-started/
installed without trouble, then tried testing as they instructed,
created src/test.php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Fields\CampaignFields;
$app_id = "{app-id}";
$app_secret = "{appsecret}";
$access_token = "{access-token}";
$account_id = "act_{{adaccount-id}}";
Api::init($app_id, $app_secret, $access_token);
$account = new AdAccount($account_id);
$cursor = $account->getCampaigns();
// Loop over objects
foreach ($cursor as $campaign) {
echo $campaign->{CampaignFields::NAME}.PHP_EOL;
}
filled in the required values. and ran the file. Getting this:
FacebookAds\CrashReporter : Enabled
FacebookAds\CrashReporter : Exception detected!
FacebookAds\CrashReporter : Successfully sent report
Fatal error: Uncaught FacebookAds\Http\Exception\AuthorizationException: Invalid OAuth access token. in C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Exception\RequestException.php:174
Stack trace:
#0 C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Client.php(215): FacebookAds\Http\Exception\RequestException::create(Object(FacebookAds\Http\Response))
#1 C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Request.php(286): FacebookAds\Http\Client->sendRequest(Object(FacebookAds\Http\Request))
#2 C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Api.php(165): FacebookAds\Http\Request->execute()
#3 C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Api.php(214): FacebookAds\Api->executeRequest(Object(FacebookAds\Http\Request))
#4 C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\ApiRequest.php(187): FacebookAds\Api->call('/act_3667840345...', 'GET', Array, Array)
#5 in C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Exception\RequestException.php on line 174
Not sure what I am doing wrong. Didnt find much by searching. Can anyone please help?
The error you get is pretty explicit: "Invalid OAuth access token"
you can check the token you supplied using Facebook's token-debbuger # https://developers.facebook.com/tools/debug/accesstoken/
most probably you are missing the "ads_management" permission.
I'm trying to make woocommerce REST API work. On first sight it apears quite simple by following the doc: https://woocommerce.github.io/woocommerce-rest-api-docs/?php#introduction.
But I struggle making it work.
Here is my code:
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'my_website_uri',
'consumer_key',
'consumer_secret',
[
'wp_api' => true,
'version' => 'wc/v2'
]
);
try{
print_r($woocommerce->get(''));
}
catch (HttpClientException $e) {
print_r($e->getMessage());
print_r($e->getRequest());
print_r($e->getResponse());
}
?>
Here is what i get:
Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: Syntax error in /Applications/MAMP/htdocs/WC_REST/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:346 Stack trace: #0 /Applications/MAMP/htdocs/WC_REST/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(385): Automattic\WooCommerce\HttpClient\HttpClient->processResponse() #1 /Applications/MAMP/htdocs/WC_REST/vendor/automattic/woocommerce/src/WooCommerce/Client.php(82): Automattic\WooCommerce\HttpClient\HttpClient->request('', 'GET', Array, Array) #2 /Applications/MAMP/htdocs/WC_REST/test.php(19): Automattic\WooCommerce\Client->get('') #3 {main} thrown in /Applications/MAMP/htdocs/WC_REST/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 346
I wrote down exactly what's in the doc.
I always got an '/wp-json/wc/v2/system_status was not found on this server.' error when enabling the output of $body as suggested above.
I fixed it with enabling permalinks (in admin-backend) which is needed for accessing a slash separated url.
I am trying to use $model->save() .. but i can't get the error messages like the documentation instead the error message appears with trace error then the script dies.
for example i am trying to use this code:
$user = new Users();
$user->name = "Name";
if(!$user->save()){
//do stuff if there is an error
}
the problem is that i always get the validation error like this and the script dies and never returns false to enter the if condition :
password is required
#0 ..../UsersController.php(106): Phalcon\Mvc\Model->save()
#1 [internal function]: ....\UsersController->createAction()
#2 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(....\UsersController), 'createAction', Array)
#3 [internal function]: Phalcon\Dispatcher->_dispatch()
#4 [internal function]: Phalcon\Dispatcher->dispatch()
#5 ....\public\index.php(41): Phalcon\Mvc\Application->handle()
#6 {main}**
I Finally found the solution, I found that somewhere in the services.php
this line was enabled and that what caused the exception to be thrown
Phalcon\Mvc\Model::setup(['exceptionOnFailedSave' => true] and after turning it to false,i can easily add my conditions and check the errors message using this code.
if ($model->save() === false) {
$messages = $model->getMessages();
foreach ($messages as $message) {
echo $message, "\n";
}
}
you can send flash error using
$this->flash->error("too bad! the form had errors");
$this->flash->success("yes!, everything went very smoothly");
$this->flash->notice("this a very important information");
$this->flash->warning("best check yo self, you're not looking too good.");
see documentation
https://docs.phalconphp.com/en/latest/reference/flash.html
I have been struggling to write a PHP script that will reboot a server using the 1.6.0 version of the Rackspace API (I can't install Composer on my server needed for 1.7.0). Unfortunately it does not want to recognize my credentials and keeps throwing this error:
Fatal error: Uncaught exception 'OpenCloud\Common\Exceptions\CredentialError' with message 'Unrecognized credential secret' in /home/sandbox/public_html/rackspaceapi/lib/OpenCloud/OpenStack.php:694 Stack trace: #0 /home/sandbox/public_html/rackspaceapi/lib/OpenCloud/Rackspace.php(66): OpenCloud\OpenStack->credentials() #1 /home/sandbox/public_html/rackspaceapi/lib/OpenCloud/OpenStack.php(714): OpenCloud\Rackspace->Credentials() #2 /home/sandbox/public_html/rackspaceapi/lib/OpenCloud/OpenStack.php(591): OpenCloud\OpenStack->authenticate() #3 /home/sandbox/public_html/rackspaceapi/lib/OpenCloud/OpenStack.php(652): OpenCloud\OpenStack->checkExpiration() #4 /home/sandbox/public_html/rackspaceapi/lib/OpenCloud/Common/Service.php(337): OpenCloud\OpenStack->serviceCatalog() #5 /home/sandbox/public_html/rackspaceapi/lib/OpenCloud/Common/Service.php(68): OpenCloud\Common\Service->getEndpoint('compute', 'cloudServersOpe...', 'ORD', 'publicURL') #6 /home/sandbox/public_html/rackspaceapi/lib/OpenCloud/ in /home/sandbox/public_html/rackspaceapi/lib/OpenCloud/OpenStack.php on line 694
Here is my PHP script (I did not include my credentials)
require_once "php-opencloud.php";
// The AUTHURL used for the US was: https://identity.api.rackspacecloud.com/v2.0/
putenv("RAX_USERNAME=$username");
putenv("RAX_API_KEY=$apikey");
putenv("RAX_TENANT_NAME=$tenantname");
putenv("RAX_AUTH_URL=$authurl");
define('USERNAME', $_ENV['RAX_USERNAME']);
define('APIKEY', $_ENV['RAX_API_KEY']);
define('TENANT', $_ENV['RAX_TENANT_NAME']);
define('AUTHURL', 'RAX_AUTH_URL');
$rackspace = new \OpenCloud\Rackspace(AUTHURL,
array( 'username' => USERNAME,
'apiKey' => APIKEY ));
$cservers = $rackspace->Compute('cloudServersOpenStack', 'ORD');
$list = $cservers->ServerList();
while($server = $list->Next()) {
if ($server->name == 'My_Server')
$server->Reboot('hard');
}
echo "Reboot sequence complete.";
Any help anyone can offer would be greatly appreciated.
I'm sorta kinda new again on the programming scene. I did some php back in college but I haven't touched it ever since and now I'm working on a side project. I'm Trying to create a simple app that will use Laravel and Facebook Login as the only source of logging into the app.
I did found some tutorials on how to do this but it seems to be outdated. (src = http://maxoffsky.com/code-blog/integrating-facebook-login-into-laravel-application/)
It seems to be all working until the Route.php part.
I have this code under the route.php
Route::get('/', function()
{
return 'Hello World';
});
Route::get('login/fb', function() {
$facebook = new Facebook(Config::get('facebook'));
$params = array(
'redirect_uri' => url('/login/fb/callback'),
'scope' => 'email', 'public_profile', 'user_friends' ,
);
return Redirect::away($facebook->getLoginUrl($params));
});
Route::get('login/fb/callback', function() {
$code = Input::get('code');
if (strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with Facebook');
$facebook = new Facebook(Config::get('facebook'));
$uid = $facebook->getUser();
if ($uid == 0) return Redirect::to('/')->with('message', 'There was an error');
$me = $facebook->api('/me');
dd($me);
});
In the said tutorial, I'm being asked to access the login/fb of the route. I tried doing this by accessing localhost/myapp/public/login/fb and it gave me an error
Whoops! Something went wrong
I tried to set the debug => true and I got a more puzzling set of errors
Symfony\Component\HttpKernel\Exception\NotFoundHttpException thrown with message ""
Stacktrace:
#11 Symfony\Component\HttpKernel\Exception\NotFoundHttpException in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:5465
#10 Illuminate\Routing\RouteCollection:match in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:4794
#9 Illuminate\Routing\Router:findRoute in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:4782
#8 Illuminate\Routing\Router:dispatchToRoute in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:4774
#7 Illuminate\Routing\Router:dispatch in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:706
#6 Illuminate\Foundation\Application:dispatch in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:687
#5 Illuminate\Foundation\Application:handle in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:7440
#4 Illuminate\Session\Middleware:handle in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:8047
#3 Illuminate\Cookie\Queue:handle in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:7994
#2 Illuminate\Cookie\Guard:handle in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:10615
#1 Stack\StackedHttpKernel:handle in D:\Xampp\htdocs\GrabATable\bootstrap\compiled.php:648
#0 Illuminate\Foundation\Application:run in D:\Xampp\htdocs\GrabATable\public\index.php:49
Can someone point to me what am I doing wrong here? Thanks in advance. :)