Sorry , I have a problem with this script :
It's for get tweet with specific id ;
$tweetlist = $twitter->get('https://api.twitter.com/1.1/statuses/show.json?id=694658584664236033');
foreach ($tweetlist->statuses as $key => $value) {
$message = utf8_decode($value->text);
echo $message;
}
Warning: Invalid argument supplied for foreach()
Thank you so much for your help
Nicolas
remove ->statuses, like:
$tweetlist = $twitter->get('https://api.twitter.com/1.1/statuses/show.json?id=694658584664236033');
foreach ($tweetlist as $key => $value) {
$message = utf8_decode($value->text);
echo $message;
}
Related
Do you know how via a web page usind the IPB APi allowinfg to doynload a file. Until now I can the see the files but it's not possible to download it. My result myfile:zip.3313213213
Thank you
Below my approach :
public function getFilesInformations(int $id)
{
$curl = curl_init( $this->communityUrl . 'api/downloads/files?id=' . $id . '&download&perPage=300');
$array = [
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "{$this->apiKey}:",
CURLOPT_USERAGENT => "MyUserAgent/1.0"
];
curl_setopt_array( $curl, $array);
$response = curl_exec( $curl );
return $response;
}
My other function to get the file info
public function insertFilesInformations(int $id): bool
{
$json_result = $this->getFilesInformations($id);
$result = json_decode($json_result, true);
$i = 1;
if (is_array($result)) {
foreach ($result as $value) {
if (is_array($value)) {
foreach ($value as $file) {
$download = $file['files'][0]['url'];
var_dum($download);
}
}
}
}
return $download
}
the result is something like that : https://localhost/forum/uploads/monthly_2019_12/myfile_zip.a1fb44a2cb001aa6f3596ec4c6dfba16
this is my code, i am trying to post tweet with image, but only text get posted?I really really want to post image as well. I am pulling my hair out for that, HELP!!?
<?php
error_reporting(E_ALL);
require_once('TwitterAPIExchange.php');
//echo 'start';
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
require_once('connect.php');
$recid=$_GET['recid'];
//echo $recid;
$dsn='mysql:host='.$hostname.';dbname=twitter_db';
try{
$dbh=new PDO($dsn,$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$stmt=$dbh->prepare("SELECT * FROM gr_twitter WHERE recid=:recid");
$stmt->execute(array(
'recid'=>$recid
));
$foundResult=$stmt->fetchAll();
$tweetmsg=$foundResult[0]['tweet'];
$tweetImage=$foundResult[0]['tweetImageName'];
$timestamp=$foundResult[0]['sentTimestamp'];
print_r($foundResult);
$stmt2=$dbh->prepare("UPDATE gr_twitter SET sent=1 WHERE recid=:recid");
$stmt2->execute(array(
'recid'=>$recid
));
}
catch(PDOException $e){}
// Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod='POST';
////images is stored in D:\Databases\RC_Data_FMS\images\Files\images\tweetImage folder
$tweetImage='D:\Databases\RC_Data_FMS\images\Files\images\tweetImage/images.jpg';
$postfields = array(
'status' => $tweetmsg,
'media' => "#{$tweetImage}"
);
/** POST fields required by the URL above. See relevant docs as above **/
//print_r($postfields).'<br />';
$twitter = new TwitterAPIExchange($Yh_settings);
$response= $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
echo "Success, you just tweeted!<br />";
var_dump(json_decode($response));
//////////////////////////////////////////////////////////////////////////
function objectToArray($d)
{
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
// return array_map(__FUNCTION__, $d);
} else {
// Return array
// return $d;
}
}
?>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I recommend you to use tmhOAuth library if you want to post images that are located in your server.
Here you have an example:
<?php
require_once('./tmhOAuth.php');
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => CONSUMER_KEY,
'consumer_secret' => CONSUMER_SECRET,
'user_token' => OAUTH_TOKEN,
'user_secret' => OAUTH_TOKEN_SECRET
));
$tweetText = 'Your text here';
$imageName = 'picture.png';
$imagePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $imageName;
$code = $tmhOAuth->request(
'POST',
$tmhOAuth->url('1.1/statuses/update_with_media'),
array(
'media[]' => "#{$imagePath};type=image/png;filename={$imageName}",
'status' => $tweetText
),
true, // use auth
true // multipart
);
?>
Hope this helps!
PHP Query:
<?php
$query = $db->prepare('SELECT * FROM Locations WHERE user_id = :id LIMIT 0, 5');
$query->bindParam(":id",$id);
$result = $query->execute();
$rows = $query->fetch();
foreach ($rows as $row) {
echo $row["timestamp"]; "<br />";
}
?>
The two rows that should be printed (timestamp):
What actually prints:
1188((22
The error within the console:
PHP Warning: Illegal string offset 'timestamp' in /Sites/pages/user_account.php on line 73 - Line 73 being the echo $row... inside the forloop.
Any help would be greatly appreciated.
You are using fetch, which retrieves a single row, instead of fetchAll:
$rows = $query->fetchAll();
You have two rows (user_id = 8)
$rows = $query->fetchAll();
For all rows
foreach ($rows as $row) {
echo $row . "<br />";
}
For 1 row , all column
while ($row = $rows) {
foreach ($row as $column) {
echo $column . "<br />";
}
}
For my school homework I have to create a function that uses trim(), htmlspecialchars() and mysql_real_escape_string() to prevent SQL- and HTML injection.
I've been trying for a while but I can't get it to work. I've tried a foreach loop and an extract function. I must be doing something wrong, or missing something.
So far, I've got this: (just to see if the variables are being processed)
foreach ($_Post as $Key => $Value) {
$$Key = $Value;
echo $$Key."<br>";
}
But it won't return anything.
I can use the trim etc on every variable on its own, but there must be a much easier way.
I've got the $_POST variables 'voorletters', 'tussenvoegsel', 'naam', 'adres', 'huisnummer' (numbers), 'telefoon' (numbers), 'postcode', 'woonplaats', 'geslacht', 'email' and 'wachtwoord' (password).
Please help me :(! I'm a beginner concerning php, so please try to explain thoroughly.
What about this
foreach($_POST as $key => $value) {
echo 'Current value in $_POST["' . $key . '"] is : ' . $value . '<br>';
$_POST[$key] = your_filter($value);
}
where your_filter() is your function calling trim, htmlspecialchars, etc. :
function your_filter($value) {
$newVal = trim($value);
$newVal = htmlspecialchars($newVal);
$newVal = mysql_real_escape_string($newVal);
return $newVal;
}
Pay attention to the variable name too which is $_POST not $_Post.
You don't need to use $$ here, you have the key name in the loop in $key and you can access/replace the value in the array with $_POST[$key]
EDIT : added an echo to print current value
EDIT2 : added an example of your_filter() function
// $_POST = array('voorletters' => '<<', 'tussenvoegsel' => '>>', 'naam' => '<<');
foreach($_POST as &$val) //pass any post value by reference
$val = mysql_real_escape_string(htmlspecialchars(trim($val)));
extract($_POST);
echo $voorletters;
echo $tussenvoegsel;
echo $naam;
foreach ($_POST as $Key => $Value) {
echo yourFunctionName($Value)."<br/>";
}
Try This...
function real_escape_and_trim($value)
{
$value = trim($value);
$value = mysql_real_escape_string($value);
return $value;
}
foreach($_POST as $key => $value)
{
$_POST[$key] = real_escape_and_trim($value);
}
$field_name = $_POST['field_name'];
I have implemented the following code below according to the documentation and can get it to connect and display user id...
<?php
define('FACEBOOK_APP_ID', '87939462878');
define('FACEBOOK_SECRET', '1ab6346bc51329831998985aba200935');
function get_facebook_cookie($app_id, $application_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $application_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
?>
<?php if ($cookie) { ?>
Your user ID is <?= $cookie['uid'] ?>
<?php } else { ?>
<fb:login-button show-faces="true" perms="email,user_birthday,user_location"></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>
<?php
$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?access_token=' .
$cookie['access_token']))->id;
print_r($user);
?>
However, if I use:
$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?wrap_access_token=' .
$cookie['oauth_access_token']))->me; //or $cookie['access_token']))->me;
register_user($user->id, $user->email, $user->name, $user->username,
$user->birthday_date);
to get the email address, I keep getting file_get_content errors. I also get errors saying it doesn't recognize the register_user function. I feel that I'm close!! Please help if possible, thanks!
I'm not familiar with the library you're using that has register_user, but is there any reason you aren't using the Facebook connect PHP SDK? You can download it at http://github.com/facebook/php-sdk and it has examples for both obtaining the auth token with extended permissions and then pulling all the user information you seem to want.