Posting a Tweet using Oauth - php

I'm trying to create a web application that will allow a user to post a tweet from a form directly on the webpage, instead of using Twitter's own pre-built pop-up. The problem is, the snippet of code that I'm seeing around the web is not working:
$message = "Hello there! This is a tweet!";
$twitterObj->post('statuses/update', array('status' => "$message"));
And when I try to execute the code I get this error:
Warning: Invalid argument supplied for foreach() in /twitter/EpiOAuth.php on line 76
Warning: http_build_query() [function.http-build-query]: Parameter 1 expected to be Array or Object. Incorrect value given in /twitter/EpiOAuth.php on line 140
I'm building off the example and using the OAuth library found at this web address:
http://www.jaisenmathai.com/articles/twitter-php-sign-in.html
Does anyone have any tips?
EDIT
Problem solved! It turns out that this was the correct statement that I needed to use:
$twitterObj->post_statusesUpdate(array('status' => 'Message goes here.'));

This is what I use:
$message = "Hello there! This is a tweet!";
$twitterObj->OAuthRequest('https://twitter.com/statuses/update.xml', array('status' => $message), 'POST');
This is with the library found here: https://github.com/abraham/twitteroauth

Related

Error connecting to getstream php

I am following the example at https://getstream.io/get_started/?language=php to understand how getstream io works. I ran into an error that got me confused.
require_once './vendor/autoload.php';
$client = new GetStream\Stream\Client('YOUR_API_KEY', 'API_KEY_SECRET');
$chris = $client->feed('user', 'chris');
// I replaced Your api key and api key secret with the one in my dashboard
// Add an activity; message is a custom field - tip: add unlimited custom fields!
$data = array(
"actor" => "chris",
"verb" => "add",
"object" => "picture:10",
"foreign_id" => "picture:10",
"message" => "Beautiful bird. Absolutely beautiful. Phenomenal bird."
);
$chris->addActivity($data);
// jack's 'timeline' feed follows chris' 'user' feed:
$jack = $client->feed('timeline', 'jack');
$jack->followFeed('user', 'chris');
// Read the 'timeline' feed for jack, chris' post will now show up:
$activities = $jack->getActivities(10);
var_dump($activities);
In my composer.json file I did this
"require": {
"get-stream/stream": "2.2.8"
}
I tried the above code on my localhost machine on windows but got this error
Fatal error: Uncaught exception 'GuzzleHttp\ExceptionConnectException' with message 'cURL error 28: Operation timed out after 0 milliseconds with 0 out of 0 bytes received (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)' in C:\xampp\htdocs\CorpersMate\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 186
GuzzleHttp\Exception\ConnectException: cURL error 28: Operation timed out after 0 milliseconds with 0 out of 0 bytes received (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in C:\xampp\htdocs\CorpersMate\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 186
any ideas guys?
You should provide guide - we have to register first to get it, it can't happen.
Change second line to
$client = new GetStream\Stream\Client(KEY, SECRET);
I later found a tweak to the problem. The problem is with the guzzle library which is trying to verify my certificate. Because I needed a way to test it on local server before moving to production server, I had to modify the client constructor in the guzzle library and that solved the problem for me.
// file name is Client.php
public function __construct(array $config = ['verify' => false]) {
if (!isset($config['handler'])) {
$config['handler'] = HandlerStack::create();
}
// Convert the base_uri to a UriInterface
if (isset($config['base_uri'])) {
$config['base_uri'] = Psr7\uri_for($config['base_uri']);
}
$this->configureDefaults($config);
}
I'll dig into why this doesn't work on Xampp for you. Can you send us your cURL options, library version, etc.?
In the meantime, based on the workflow you've built:
build a feed for Chris
build an activity and put it on Chris' feed
build a feed for Jack
Jack follows Chris' feed
read Jack's feed and expect to see Chris' activity
... Jack will need to specify a number of activities to copy when following Chris' feed, otherwise Jack will only see updates from that point on; Jack will never see "picture:10" from Chris. There's a third optional parameter you can send through followFeed() that specifies how many items to copy to Jack's feed when you start following:
$jack->followFeed('user', 'chris', 100);
Or you can move step 4 between step 1 and 2. If Jack follows Chris before Chris adds a photo, it should show up on Jack's feed.

Error when using Facebook's PHP SDK to send notification

I'm sending a Facebook notification using PHP but I've run into a problem I can't seem to solve with Facebook's docs.
The notification informs a person that their friend has beaten them in a game, This is the PHP code that sends the message:
<?php
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '{123456789}',
'app_secret' => '{1a2b3c4d5f6g7h8i9j}',
'default_graph_version' => 'v2.3',
]);
//This is an array of ID numbers received from an Ajax post
$data = json_decode(stripslashes($_POST['data']));
//This the ID number of the sender of the notifications
$senderid = $_POST['senderid'];
echo $senderid;
foreach($data as $d){
$request = $fb->post('/'.$d.'/notifications', array( 'template' => '#['.$senderid.'] just beat you!'), '123456789|1a2b3c4d5f6g7h8i9j');
}
?>
This code is taking an array of ID numbers ($data) and sending the notification to each one. The senderid is the ID of the sender, and according to the docs here: https://developers.facebook.com/docs/games/services/appnotifications typing something like #[12625234523] just beat you! should automatically put the name of the person who owns that ID in place, so the notification should read something like "John Doe just beat you".
But this isn't working for me. I'm certain that the sender's ID is successfully being posted to the PHP file because it's appears in the console when I echo $senderid. And when I type in the sender's ID manually into the 'template' part of the request I'm getting the same error in the console. The error is:
Fatal error: Uncaught exception 'Facebook\Exceptions\FacebookServerException' with message 'An unknown error has occurred.' in /home/mywebsite/public_html/Games/Barre/facebook-sdk-v5/Exceptions/FacebookResponseException.php:105
Stack trace:
0 /home/mywebsite/public_html/Games/Barre/facebook-sdk-v5/FacebookResponse.php(210): Facebook\Exceptions\FacebookResponseException::create(Object(Facebook\FacebookResponse))
1 /home/mywebsite/public_html/Games/Barre/facebook-sdk-v5/FacebookResponse.php(255): Facebook\FacebookResponse->makeException()
2 /home/mywebsite/public_html/Games/Barre/facebook-sdk-v5/FacebookResponse.php(82): Facebook\FacebookResponse->decodeBody()
3 /home/mywebsite/public_html/Games/Barre/facebook-sdk-v5/FacebookClient.php(225): Facebook\FacebookResponse->__construct(Object(Facebook\FacebookRequest), '{"error":{"mess...', 500, Array)
4 /home/mywebsite/public_html/Games/Barre/facebook-sdk-v5/Facebook.php(504): Facebook\FacebookClient->sendRequest(Object(Facebook\FacebookRequest))
5 /home/mywebsite/public_ht in /home/mywebsite/public_html/Games/Barre/facebook-sdk-v5/Exceptions/FacebookResponseException.php on line 105
I'm sure that the array of IDs is working correctly, as everything works perfectly when I remove the $senderid from the message template. This isn't ideal though as I would like to tell the player who has beaten them, instead of giving them a message like "You've just been beaten." I would like to include the name of the sender in the message/template but the method given in the Facebook document above isn't working, maybe it's only possible to use the #[12425232] method when typing into Facebook's Graph API Explorer?
I'd really appreciate any help anyone could give me with this problem.
Thank you in advance!
Please use GET or REQUEST instead of POST for data and senderid

PHP : Google Oauth - error in JWT - explode() expects parameter 2 to be a string

I'm in the process of modifying a login script to use Google OAuth authentication using the Google API PHP Client Library on PHP v5.6.
Code is based on the idtoken example on GitHub
It was almost there until I get the following error twice:
PHP Warning: explode() expects parameter 2 to be string, array given in /...../cendor/firebase/php-jwt/src/JWT.php on line 65.
Using this method as main objective is to authenticate users of certain Google Apps for Work domains using the hd claim in the token.
My code section is as follows:
if($client->getAccessToken()){
$token = $client->getAccessToken();
$ticket = $client->verifyIdToken($token);
if($ticket) {
$data = $ticket->getAttributes();
$userId = $data['payload']['sub'];
$email = $data['payload']['email'];
$hd = $data['payload']['hd'];
etc...
Scope has been set to 'email'.
Any ideas would be greatly appreciated.

yahoo oauth $user->getProfile(); returns null

I'm using php and downloaded the yahoo library to import yahoo user info.
I've already finished it and it has been working flawlessly, but now it has started to return:
Notice (8): Trying to get property of non-object
[APP/Vendor/Yahoo/Yahoo.inc, line 1036]
This notice appears after logging in through yahoo. When I debug $user->getProfile() it returns null.
I didn't change anything in my code that's why i don't have any idea what is causing it.
Please help!
This is the relevant part of my code:
App::import('Vendor', 'YahooInc', array('file' => 'Yahoo/Yahoo.inc'));
$session = YahooSession::requireSession($consumer_key,$consumer_secret,$app_id);
if (is_object($session)) {
$yahooUser = $session->getSessionedUser();
$yahooProfile = $yahooUser->getProfile();
}
You have to replace your current Yahoo Api library with the below github branch.
https://github.com/syamvilakudy/yos-social-php

Not able to upload object to google cloud storage using google API

I am using the following link for using the Google Cloud Storage :
Google CLoud
I want to upload an object using insert function given in the above API.I am using PHP. The code i am using is as follows :
$StorageService = new Google_StorageService($client);
$objects = $StorageService->objects;
$gso = new Google_StorageObject();
$gso->setName('myobj');
$postbody = array(file_get_contents('buc.jpg'));
$resp = $objects->insert('mybucket', $gso, $postbody);
But i m getting Error as :
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/storage/v1beta1/b/mybucket/o: (400) Required' in /home/www/public_html/abc/google-api-php-client/src/io/Google_REST.php on line 66
What am i doing wrong please help me out..
There is some problem with the way i am sending the parameters. Please if anyone knows help me..
There is an example here :
Example
But that is in Java Please help me to do the same thing in PHP. Please help me out.
I figured out the answer. The code to be used is as follows :
$objects = $StorageService->objects;
$postbody = array('data' => file_get_contents('buc.jpg'));
$gso = new Google_StorageObject();
$gso->setName('mybuc');
$resp = $objects->insert('mybucket', $gso ,$postbody);
print_r($resp);
and then it works ..
I was not setting the parameter "data" in an array to be passed as a third parameter of the function. I Found out and it worked.
Buckets share a global namespace. You have to create your bucket before storing objects in it, and "mybucket" is not an available name.
If you go to the GCS browser at https://storage.cloud.google.com/, you can create your bucket there and then reference the existing bucket in your PHP code.
I had to add uploadType to get it to work as mentioned here: https://cloud.google.com/storage/docs/json_api/v1/objects/insert
$postbody = array('uploadType' => 'media', 'data' => file_get_contents('buc.jpg'));
Where is the data parameter documented?

Categories