I have a problem, I'm trying to get some data for a unique link to my site.
When people are viewing eg: video.php?id=23 i want the script to get the data for that site using $_GET['id'].
Here's my script, and I've tried everything. Hope you can help me!
<?php
$vidurl = $_GET['id'];
function fb_count() {
global $fbcount;
$facebook = file_get_contents('http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.fniis.dk/video.php?id=$vidurl');
$fbbegin = '<share_count>'; $fbend = '</share_count>';
$fbpage = $facebook;
$fbparts = explode($fbbegin,$fbpage);
$fbpage = $fbparts[1];
$fbparts = explode($fbend,$fbpage);
$fbcount = $fbparts[0];
if($fbcount == '') { $fbcount = '0'; }
}
fb_count();
?>
The problem is that it wont let me print the $vidurl, it doesnt seem to work, because it is only getting data from the following link : fniis.dk/video.php?id= and not eg: fniis.dk/video.php?id=123
You have a couple of problems in your code.
First, you won't be able to access $vidurl in fb_count() unless you specify it as global inside fb_count():
global $vidurl;
It is recommended that you pass $vidurl as a parameter fb_count() instead of using global.
Second, your concatenation of $vidurl in file_get_contents is incorrect. You should be using double quotes instead of single quotes so $vidurl will be processed by PHP. It also wouldn't hurt to use urlencode() here:
// note: using single quotes here and just concatenating with "."
$facebook = file_get_contents('http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.fniis.dk/video.php?id=' . urlencode($vidurl));
That should get your code working.
Related
I'm trying to append my variable that displays the count after my links, but I already have code I need replacing links to redirect them to a redirect page.
This is my code currently:
$this->post['message'] = str_replace('href="', 'href="./out.php?link=', $this->post['message']);
$this->post['signature'] = str_replace('href="', 'href="./out.php?link=', $this->post['signature']);
So all the links get redirected to out.php
(small additional question how can i make this not effect img href's?)
So back on topic, what I'm trying to do is add $someVariable[value] after my links, also not effecting images.
The variable would show plain text (numbers)
and for example the end result would look like:
Google(##)
where ## represents $someVariable
DOM confuses me, and I cant think of any other way to do it, so I need some help.
1
$ur = "./out.php";
$k = str_shuffle("7dfg9bf09cv04e79507e197a1dtdy8j3");
$web = $ur."/?link=".$k;
echo $web;
Here is an example.
2
Escaping quotation marks in PHP
<?php
$ur = "./out.php";
// $k = $this->post['message'];
$k = "hello";
$web = "/?link=".$k;
$li = "".$k."(".$k.")";
echo $li."\n";
?>
<?php
session_start();
require_once("twitteroauth/twitteroauth.php");
$apikey="xxx";
$apisecret="xxx";
$accesstoken="xxx";
$accesssecret="xxx";
$connection = new TwitterOAuth($apikey, $apisecret, $accesstoken, $accesssecret);
$response=
$connection->get("https://api.twitter.com/1.1/statuses/home_timeline.json?count=10");
foreach ($response as $tweet) {
$favorites=$tweet->favorite_count;
if ($favorites>1) {
$embed = $connection->get("https://api.twitter.com/1.1/statuses/oembed.json?id='.$tweet->id.'");
print_r($embed->html);
}
}
?>
It works with a sample Tweet id# put in place of $tweet->id and I've tried a lot of different syntax to try and make this mini-app work. I'm new, and Thanks.
Try taking out the dots that you've got in there before and after $tweet-id. There's 2 ways that you could put the value in there in-line:
$embed = $connection->get("https://api.twitter.com/1.1/statuses/oembed.json?id='$tweet->id'");
(When surrounding your string with double quotes ", you can enter variables directly into it like any other text.
or
$embed = $connection->get("https://api.twitter.com/1.1/statuses/oembed.json?id='".$tweet->id."'");
(In this method, you make it explicitly obvious that you're adding a variable in there. Note that when using single quotes ', this method must be used.
I have a sql query that I store in a variable and I displayed. I get the contents of this with file_get_contents from another file, I would like to recover some of this code (which is html) in order to make link. More precisely retrieve the id.
My api.php
$base = mysql_connect ('localhost','root','');
mysql_select_db('administrations', $base);
if(isset($_GET['cp']))
{
$sql = 'SELECT NOM_organisme, ID_organisme
FROM organismes
WHERE code_postal LIKE "%'.$_GET['cp'].'%"
ORDER BY NOM_organisme;';
$req = mysql_query($sql) or die('SQL Error !<br>'.$sql.'<br />'.mysql_error());
}
while ($data = mysql_fetch_array($req))
{
echo '<p id="'.$data['ID_organisme'].'"'.
$data['NOM_organisme'].'</br>'.
$data['ID_organisme'].'</p></br>';
}
I want to get the id="I WANT THIS".
And my index.php (part of my code that retrieves the contents).
if(isset($_POST['cp']))
{
$api = "http://mywebsite.fr/api.php?cp=".$_POST['cp'];
$var = file_get_contents($api);
echo $var;
}
How can I get the id="" in my index.php ?
please look at php get documentation. you need to link to your script with url parameters and access them in your php code.
http://php.net/manual/en/reserved.variables.get.php
echo ''.$data['NOM_organisme'].'</br>'.$data['ID_organisme'].'</br>';
php
if(isset($_GET['id']))
{
$api = "http://mywebsite.fr/api.php?cp=".$_GET['id'];
$var = file_get_contents($api);
echo $var;
}
if you dont want to use url parameter you can use post values
http://php.net/manual/en/reserved.variables.post.php
I understand what your trying to do, but dont find it logical without knowing the purpose of this tiny code :)
Do you have a link or some sort?
Basicly what i should do is:
$base = mysql_connect ('localhost','root','');
mysql_select_db('administrations', $base);
if(isset($_POST['cp']))
{
$sql = 'SELECT NOM_organisme, ID_organisme FROM organismes WHERE code_postal LIKE "%'.$_GET['cp'].'%" ORDER BY NOM_organisme;';
$req = mysql_query($sql) or die('SQL Error !<br>'.$sql.'<br />'.mysql_error());
while ($data = mysql_fetch_array($req))
{
echo '<p id="'.$data['ID_organisme'].'"'.$data['NOM_organisme'].'</br>'.$data['ID_organisme'].'</p></br>';
}
} else {
echo 'show something else';
}
If I get you correctly, you are
Sending a GET request in index.php using file_get_contents() to your website.
The website (api.php) performs an SQL query and prints the result in HTML.
index.php takes this HTML output and stores it in the variable $var.
You want to retrieve all values contained inside the id attribute of the paragraph.
In this case, you probably want to use regular expressions. preg_match_all seems to be appropriate. It should work for you like this:
$out = array();
preg_match_all("/id=\"([^\"]*?)\"/U", $var, $out);
foreach ($out as $value) {
echo 'I found some id ' . htmlspecialchars($out[$value][2]) . '<br />';
}
And additionally:
A decent HTML parser would be much more appropriate in this case (eg. it would not match id="X" in flow text).
Your PHP code is vulnerable to SQL injections.
You should sanitize plain text to HTML appropriately.
First of all, you should try to display your API reply as a JSON-string, this is much more convenient.
If you still want to use your api.php, you first need to close your opening paragraph! You did forget a '>'!
echo '<p id="'.$data['ID_organisme'].'">'.
$data['NOM_organisme'].'</br>'.
$data['ID_organisme'].'</p></br>';
Then you need to parse your paragraph.
You can do it like that:
if(isset($_POST['cp']))
{
$api = "http://mywebsite.fr/api.php?cp=".$_POST['cp'];
$var = file_get_contents($api);
preg_match("#<p id='(.*)'#", $var, $matches);
id = $matches[1];
echo $id;
}
I have been attempting to use steam api to display a persons name. When I insert there steamid in directly it works however, when I try to use a variable it doesn't work. The thing that confuses me is that I have this exact same code for my queue that works however this does not work. I have looked around and I believe I am doing this right but for some reason it is not working. Any help here would be great. Thanks.
<?
$name = 76xxxxxxxxxxx;
$response = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/? key=xxx&steamids={$name}&format=json";
$middle = (file_get_contents($response));
$json = json_decode($middle, true);
$b = $json['response']['players'][0]['personaname'];
print_r($b);
?>
Your $name variable needs to be wrapped in quotes.
$name = "76xxxxxxxxxxx";
This will properly create your response variable
I'm loading an XML file into the page and want to get information from the URL parameter and put it in the url for the XML file that simplexml is laoding. is this possible?
Here is the current code:
$gid = $_GET['gid'];
$gach = simplexml_load_file('http://xml.com/GIDHERE/page.html?xml=1');
How can I put $gid where it says GIDHERE ?
$gach = simplexml_load_file('http://xml.com/' . rawurlencode($gid) . '/page.html?xml=1');
You might also want to validate the input before using it, e.g.:
if (!preg_match('{^[1-9][0-9]*$}', $gid)) die;
If $gid is always an integer.
$gach = simplexml_load_file("http://xml.com/$gid/page.html?xml=1");
you have to use double quote, single quote disable variable parsing
and watch out for putting unchecked variable into url, you may end with exploits!
if gid is integer, just force it to be so by doing this
$gid = (int) $_GET['gid'];