Sharing a URL within a tweet - php

I want a link to appear within a twitter tweet. At the moment the raw HTML comes out.
click here to test
Here is my code.
$url = 'www.google.com'
$text = urlencode( "click here to test" )
$share = 'http://twitter.com/share';
$url = '?url=' . $url;
$text = '&text=' . $title;
$hashtag = '&hashtags=' . $hashtag;
$url = $share . $url . $text . $hashtag;
Without urlencode() the actual twitter icon then spits out HTML text everywhere on my page.

twitter auto converts url. but i dont think you can do anything more than what stated here because of anti spam policy. https://support.twitter.com/articles/78124

Related

How to construct url with http_build_query, but display values without variables?

I'm trying to generate an URL from all values of two arrays by using http_build_query:
Array 1:
$server = array($_GET["server"]);
Array 2:
$data = array($_GET["z_koord"],
$_GET['x_koord'],
$_GET["y_koord"],);
The code for generating URL I currently have written:
$server = array(''=>$_GET["server"]);
$data = array($_GET["z_koord"],
$_GET['x_koord'],
$_GET["y_koord"],);
$url = '.tile.openstreetmap.org';
$saite = http_build_query($server). $url ."/". http_build_query($data,'','/').".png";
Here's the URL made of code above:
=c.tile.openstreetmap.org/0=6/1=90/2=110.png
Here's the structure of url I'm trying to make:
c.tile.openstreetmap.org/6/90/110.png
I have reviewed some other posts about this topic like this one and this, but those posts aren't completely useful for solving my problem.
So I hope someone with greater knowledge could show me a solution or at least a hint how to get closer to solution.
You could use implode():
$server = $_GET["server"];
$data = [$_GET["z_koord"],
$_GET['x_koord'],
$_GET["y_koord"]];
$url = '.tile.openstreetmap.org';
$saite = "$server/$url/" . implode('/', $data) . ".png";
I'm not sure about some things in this code, but the implode() should do the job.
You are using http_build_query in the wrong way. You just don't need that. There are 2 options, you may use any one of them.
Use implode(), the simplest way to do the job.
$server = array(
'' => $_GET['server']
);
$data = array(
$_GET['z_koord'],
$_GET['x_koord'],
$_GET['y_koord'],
);
$url = $server . '.tile.openstreetmap.org';
$saite = $url . '/' . implode("/", $data) . '.png';
Directly create the URL using the Parameters as shown here:
$url = '.tile.openstreetmap.org' .;
$saite = $_GET['server'] . $url . '/' . $_GET['z_koord'] .'/'. $_GET['x_koord'] . '/'.$_GET['y_koord'] . '.png';

How to cache multiple imdb rating results

I'm trying to make a movie list where is movie name and I'm trying to get imdb ratings, from url (database) and code works. But my problem is that if I have 50 movies in the list, this code makes 50 different omdb api calls and it slows down my site very well.
So how I can store/cache ratings? I'm not good for cache thins at all. Thanks :)
if (($row["imdb"] != "") AND (strpos($row["imdb"],'imdb')) AND
(strpos($row["imdb"],'title'))) {
$imdbID = ltrim(strrchr($row["imdb_url"],'tt'),'tt');
$imdbID = preg_replace("/[^A-Za-z0-9]/", "", $imdbID);
$omdbapi_key = 'API_KEY';
$url = file_get_contents("https://www.omdbapi.com/?i=tt" . $imdbID . "&apikey=" . $omdbapi_key);
$omdb = json_decode($url, true);
$imdb_rating = "<a target='_blank' href='https://www.imdb.com/title/tt" . $imdbID . "'>Rating: " . $omdb['imdbRating'] . " / 10</a>";
Now I can write cache file for just one movie. I don't know how I can add multiple movie api calls in one file. I have form where I add movie (Title, imdb url). I think I need rewrite whole file when I add new movie.
Update:
$cacheFile = 'cache' . DIRECTORY_SEPARATOR . md5($url);
if( file_exists($cacheFile))
{
$omdb = json_decode(file_get_contents($cacheFile), true);
}
else
{
$url = file_get_contents("https://www.omdbapi.com/?i=tt".$imdbID."&apikey=" . $omdbapi_key);
file_put_contents($cacheFile, $url);
$omdb = json_decode($url, true);
}

Detection - the title of the URL and the URL

How to detect, if there is any URL in the text and title it has (if any)?
If there is one, then it should change the URL:
from: http://stackoverflow.com
into:
<detected:url="http://stackoverflow.com"/>
I need also to retrieve titles from external links like this example:
<title:http://stackoverflow.com/="the actual title from the stackoverflow"/>
This is for single URL case:
$url = "http://www.stackoverflow.com/";
$check_result = get_detected_and_title( $url );
function get_detected_and_title( $url )
{
$detected = '<detected:url="'.$url.'"/>';
$title = '';
$tmp_html = file_get_contents( $url );
preg_match('/<title>(.*)<\/title>/', $tmp_html, $res);
$title = '<title:'.$url.'="'.$res[1].'"/>';
return array( $detected, $title );
}
Actually, after looking through SO's pages, I think this is more close to what you looking for. Although it needs some adjustment: How to mimic StackOverflow Auto-Link Behavior

Retrieve a Get Http request in php

I need to retrieve a get http request in php and store it in a variable.
I need to execute the following:
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&
grant_type=client_credentials
I know this is simple. just not able to get my head around it.
$content = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials');
Within the Open Graph protocol page on Facebook, there is an example within the documentation coded using PHP: http://developers.facebook.com/docs/opengraph/
<?php
$ogurl = "INSERT_YOUR_OG_URL_HERE";
define(FACEBOOK_APP_ID, "YOUR_APP_ID_HERE");
define(FACEBOOK_SECRET, "YOUR_SECRET_KEY_HERE");
$mymessage = "Hello World!";
$access_token_url = "https://graph.facebook.com/oauth/access_token";
$parameters = "grant_type=client_credentials&client_id=" . FACEBOOK_APP_ID ."&client_secret=" . FACEBOOK_SECRET;
$access_token = file_get_contents($access_token_url . "?" . $parameters);
$apprequest_url = "https://graph.facebook.com/feed";
$parameters = "?" . $access_token . "&message=" . urlencode($mymessage) . "&id=" . $ogurl . "&method=post";
$myurl = $apprequest_url . $parameters;
$result = file_get_contents($myurl);
// output the post id
echo "post_id" . $result;
}
?>
The key line in actually making the call being:
$result = file_get_contents($myurl);
There is also a good amount of other information about the resulting object you get back there that would be good to take a look into.
Hope this is helpful.
if ($fp = fopen('https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&
grant_type=client_credentials', 'r')) {
$content = '';
// keep reading until there's nothing left
while ($line = fread($fp, 1024)) {
$content .= $line;
}
// do something with the content here
// ...
} else {
// an error occured when trying to open the specified url
}
You mean something like
$client_id = $_GET['client_id'];
$client_secret = $_GET['client_secret'];
$grant_type = $_GET['grant_type'];
?
Or rather something like
$content = file_get_contents($url);
?
Use the following
$id = $_GET['client_id'];
$type = $_GET['grant_type'];
$secret = $_GET['client_secret'];
Hope this helps you

Need to validate "last tweet" script

Have been searching for a solution for hours.
My entire WordPress theme validates, except this script I'm using to receive the last tweet:
<?php
$twitterUsername = get_option('of_twitter_username');
$username = $twitterUsername; // Your twitter username.
$prefix = ""; // Prefix - some text you want displayed before your latest tweet.
$suffix = ""; // Suffix - some text you want display after your latest tweet.
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace("<", "<", $tweet);
$tweet = str_replace(">", ">", $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>
The error, it seems, is:
$tweet = str_replace(">", ">", $tweet);
Not sure how to fix this.
Thanks for any help.
Replace the two str_replace calls with:
$tweet = html_entity_decode($tweet);
Maybe a simplier way (you don't need to parse) is to load http://search.twitter.com/search.json?q=from:the_username and make a json_decode of the result.
Then you can get the last tweet easily.

Categories