I need to extract the thumbnail image from a video to facebook.
I should do it via json, the fact is that I can not extract the image to 720px.
I did so, but I get nothing, no errors or anything.
<?php
/*
10152765849330530
https://www.facebook.com/video.php?v=10152765849330530&set=vb.48166220529&type=2&theater
*/
$id = "10152765849330530";
$xml = #file_get_contents('https://graph.facebook.com/' . $id);
$result = #json_decode($xml);
//var_dump($result);
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
$result = $result->format->picture;
echo $result;
?>
Ideas?
The link is to be extracted:
https://fbcdn-vthumb-a.akamaihd.net/hvthumb-ak-xfp1/v/t15.0-10/10470126_10152765850455530_10152765849330530_61772_571_b.jpg?oh=ea81bafb0b88da98068fe8697b058bd0&oe=5493EF9F&gda=1422872671_cf3d63eef79db332df7a30c8041abdd9
Just tried to play with your code, works now -
<?php
/*
10152765849330530
https://www.facebook.com/video.php?v=10152765849330530&set=vb.48166220529&type=2&theater
*/
$id = "10152765849330530";
$xml = file_get_contents('http://graph.facebook.com/' . $id);
$result = json_decode($xml);
echo "<pre>";
echo $result->format[0]->picture;
?>
To fetch the 720px sized picture -
<?php
/*
10152765849330530
https://www.facebook.com/video.php?v=10152765849330530&set=vb.48166220529&type=2&theater
*/
$id = "10152765849330530";
$xml = file_get_contents('http://graph.facebook.com/' . $id);
$result = json_decode($xml);
echo "<pre>";
echo $result->format[3]->picture;
?>
Related
I am having trouble echoing row data within the page I want to print it out to.
My function works, but only echoes the information because it is local (within the same function).
I'm trying to get this function to echo the database's information to another .php file (same program).
public function findByUsername($username) {
$sql = 'SELECT * FROM events WHERE username=? ';
$statement = DatabaseHelper::runQuery($this->connection, $sql, Array($username));
while ($row = $statement->fetch()) {
echo $row['username'] . "<br />";
echo $row['date'] . "<br />";
}
}
Here is the part of the other file I need to print the information from the function to:
<?php
if (isset($_SESSION["username"])) {
$eventDAO = new EventDAO($connection);
$event = $eventDAO->findByUsername($_SESSION["username"]);
foreach((array)$event as $e) {
echo $e->getUsername() . ' ' . $e->getDate() . '<br>';
}
}
?>
I'm trying to output the username/date.
Not 100% on this concept.
If you're simply trying to output username/date from one file to another, try using sessions.
public function findByUsername($username) {
$sql = 'SELECT * FROM events WHERE username=? ';
$statement = DatabaseHelper::runQuery($this->connection, $sql, Array($username));
while ($row = $statement->fetch()) {
echo $row['username'] . "<br />";
echo $row['date'] . "<br />";
$_SESSION['getuname'] = $row['username'];
$_SESSION['getdate'] = $row['date'];
}
}
You can then output the information on another php file simply by echoing it out.
echo "Username is ". $_SESSION['getuname'];
I was experimenting if I could use a mySQL database to store CSS settings. I set up a simple database "colors" with one table "color" that had simple structure tag and color columns. In that, one row is h1 => red.
<?php
//function to dynamically change CSS
$tag = 'h1';
$q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
echo $q . "<br>";
$query = mysqli_query($link, $q);
if ($row = mysqli_fetch_assoc($query))
{
echo $row['color'];
} else
{
echo "error - no such tag";
}
?>
When I tried to convert to a function, the code does not work at all.
<?php
//function to dynamically change CSS
function getCSS($tag)
{
$tag = 'h1';
$q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
echo $q . "<br>";
$query = mysqli_query($link, $q);
if ($row = mysqli_fetch_assoc($query))
{
echo $row['color'];
} else
{
echo "error - no such tag";
}
}
getCSS('h1');
?>
Help please?
My guess is that in
$query = mysqli_query($link, $q);
$link goes out of scope and is empty. You should pass it to the function as well.
For the record: using $tag without escaping could be an sql injection attack possibility.
in function, there is no $link, you shoud define it as a global variable.
At the start of your function add a reference to your global DB link:
function getCSS($tag) {
global $link;
...
This should work:
<?php
$link = mysqli_connect('server_host', 'user', 'password', 'database') OR die('Could not connect because: '.mysqli_connect_error());
//function to dynamically change CSS
function getCSS($link, $tag){
$q = 'SELECT * FROM colors WHERE tag = "' . $tag . '"' ;
$r = mysqli_query($link, $q);
if(mysqli_num_rows($r)>0){ // check if there are results
while($row = mysqli_fetch_assoc($r)){
//echo '<pre>';
//print_r($row); // print the result array for debugging
//echo '</pre>';
echo $row['color'] . '<br />';
}
return $row;
} else { // if no result is found
echo 'No such tag';
}
}
// test it:
echo '<br />if tag is h1<br />';
getCSS($link, 'h1');
echo '<br />if tag is h2<br />';
getCSS($link, 'h2');
?>
Please check below code and help me out if anything is going wrong in image display code
Untitled Document
<body>
<p>how to display image</p>
<p>image is not seeingd</p>
<?php
$con= mysql_connect("localhost","root","");
$d= mysql_select_db("matrimonial",$con);
$id=$_REQUEST['id'];
$sql = "select * from advertiesment where S_NO ='1'" or die(mysql_error());
$result = mysql_query($sql,$con);
if($rows=mysql_fetch_assoc($result))
{
$image = $rows['filepath'];
echo "<img style='width:150px;height:150px' src='$image'>";
echo "<br>";
}
?><p>how to display image</p>
</body>
</html>
if($rows=mysql_fetch_assoc($result))
The condition is bad. If you have more results from SQL query,you need to use while.
while ($rows = mysql_fetch_assoc($result)) {...
If you have only one, remove just if:
$rows = mysql_fetch_assoc($result);
echo ...
EDIT:
$image = $rows['filepath'] . '/' . $rows['filename'];
Dont use :
if($rows=mysql_fetch_assoc($result))
{
$image = $rows['filepath'];
echo "<img style='width:150px;height:150px' src='$image'>";
echo "<br>";
}
use this directly
$rows=mysql_fetch_assoc($result)
$image = $rows['filepath'];
echo "<img style='width:150px;height:150px' src='$image'>";
echo "<br>";
Hiya I am getting information from my database to present onto the webpage, everything is correct with connection as the information is present on the page.
But when it comes to the pictures they come up with a little box of where they are meant to be.
Heres the code I have:
<?php
error_reporting(0);
require './db/connect.php';
include './includes/header.php';
?>
<h2>Production</h2>
<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){
$Image = $rows['Image'];
while($row = $result->fetch_object()){
echo '<pre>'.'<img class="productionimages" src="path/'.$Image.'" />',' '
,$row->ProductionName,' ',$row->ProductionType, '</pre>';
}
$result->free();
}
}
echo $result;
include './includes/footer.php';
?>
Heres a picture below of what appears on the screen.
You need to set $Image within the while loop and $rows['Image'] should be $row->Image, like so.
<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
$Image = $row->Image;
echo '<pre>'.'<img class="productionimages" src="path/'.$Image.'" />',' '
,$row->ProductionName,' ',$row->ProductionType, '</pre>';
}
$result->free();
}
}
echo $result;
include './includes/footer.php';
?>
I tested my jsno example here:
data.php
<?php
ob_start();
echo "string1";
$div1 = ob_get_clean();
ob_start();
echo "string2";
$div2 = ob_get_clean();
$resultArray = array("resultOne" => $div1,"resultTwo" => $div2);
echo json_encode($resultArray);
?>
start.php
xmlhttp.onreadystatechange=function()
{
var div1=document.getElementById("myDiv1");
var div2=document.getElementById("myDiv2");
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var data=JSON.parse(xmlhttp.responseText);
//document.getElementById("myDiv1").innerHTML=xmlhttp.responseText;
div1.innerHTML=data['resultOne'];
div2.innerHTML=data['resultTwo'];
}
}
This gives correct result and show data.
Now I tried to implement on my actual application.
login.php (verify user login, if success) -> start.php (loaded into login.php through ajax) -> data.php (returns json array values as in above case)
just difference is data.php is in following manner:
<?php
$url = $_POST['url'];
$user_id = $_POST ['userid'];
if(isset($_POST['rate']))
{
$rate =$_POST['rate'];
}
else
$rate = 0;
$data = file_get_contents($url);
function get_title($html)
{
return preg_match('!<title>(.*?)</title>!i', $html, $matches) ? $matches[1] : '';
}
function get_logo($html)
{
preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg)\b/', $html, $matches);
//echo "mactch : $matches[0][0]";
return $matches[0][0];
}
function plaintext($html)
{
// remove comments and any content found in the the comment area (strip_tags only removes the actual tags).
$plaintext = preg_replace('#<!--.*?-->#s', '', $html);
// put a space between list items (strip_tags just removes the tags).
$plaintext = preg_replace('#</li>#', ' </li>', $plaintext);
// remove all script and style tags
$plaintext = preg_replace('#<(script|style)\b[^>]*>(.*?)</(script|style)>#is', "", $plaintext);
// remove br tags (missed by strip_tags)
$plaintext = preg_replace("#<br[^>]*?>#", " ", $plaintext);
// remove all remaining html
$plaintext = strip_tags($plaintext);
return $plaintext;
}
function trim_display($size,$string)
{
$trim_string = substr($string, 0, $size);
$trim_string = $trim_string . "...";
return $trim_string;
}
$title = get_title($data);
$logo = get_logo($data);
$title_display = trim_display(30,$title);
$content = plaintext($data);
$Preview = trim_display(100,$content); //to Show first 100 char of the web page as preview
function MakeTinyUrl($url)
{
return md5($url);
}
$hash = MakeTinyUrl($url);
ob_start();
$con = mysqli_connect('127.0.0.1', 'root', '', 'mysql');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
$content=mysqli_real_escape_string($con,$content);
$Preview=mysqli_real_escape_string($con,$Preview);
$title_display=mysqli_real_escape_string($con,$title_display);
$result = mysqli_query($con,"SELECT COUNT(*) as val FROM post_data WHERE url ='".$url."' and userid='".$user_id."'");
$bool= mysqli_fetch_assoc($result);
if($bool['val'])
{
echo '<div style="clear:both;"><i>You have already worked on this url..</i> </div>';
}
else
{
$insertQuery = "INSERT INTO post_data(`userid`, `url`, `desc`, `preview`, `img_url`, `title` ,`hash`) VALUES ('".$user_id."','".$url."','".$content."','".$Preview."','".$logo."','".$title_display."','".$hash."')";
if (!mysqli_query($con,$insertQuery))
{
die('Error: ' . mysqli_error($con));
}
}
$result = mysqli_query($con,"SELECT * FROM post_data WHERE userid ='".$user_id."' and url='".$url."'");
//This will fetch only one row from db
while ($row = #mysqli_fetch_array($result))
{
$title = $row['title'];
$url = $row['url'];
$preview = $row['preview'];
$image = $row['img_url'];
}
//Update Rate value in table
$update = "update post_data set rate='".$rate."' where url='".$url."'";
if (mysqli_query($con, $update))
{
//echo "updated";
}
else
{
//echo "Not updated";
}
?>
<html>
<body>
<a class="fragment" href="google.com">
<div>
<span id='close' onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;'>x</span>
<img src ="<?php echo $image ?>" height="116" width="116" alt="some description"/>
<h3><?php echo $title ?></h3>
<h4><?php echo $url ?> </h4>
<p class="text"> <?php echo $preview ?>
</p>
</div>
</a>
<?php
$div1 = ob_get_clean();
ob_start();
$result = mysqli_query($con,"SELECT * FROM post_data WHERE userid ='".$user_id."'");
echo "Records for user : $user_id"; echo "<br/>";
while ($row = #mysqli_fetch_array($result))
{
$title = $row['title'];
$url = $row['url'];
$preview = $row['preview'];
$image = $row['img_url'];
echo "Title: $title"; echo '<br/>';
echo "URL: $url"; echo '<br/>';
echo "Preview : $preview"; echo '<br/>';
echo "Image url: $image"; echo '<br/>';
//echo '</br>';
}
$div2 = ob_get_clean();
$resultArray = array("resultOne" => $div1,"resultTwo" => $div2);
echo json_encode($resultArray);
?>
</body>
</html>
I get error message:
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
var data=JSON.parse(xmlhttp.responseText);
You can validate your json responses using below url, so that you can conclude your results some point,
$resultArray = array("resultOne" => $div1,"resultTwo" => $div2);
echo json_encode($resultArray);
Ref: http://jsonformatter.curiousconcept.com/#jsonformatter