Getting page content from vk.com - php

I would like to fetch the content of the page from vk.com.I'm using php and i've got the page contents but however its not the appropriate contents.For example when i get the contents from vk.com/video56612186_167049188 is should be getting the video details completely at the bottom but i'm getting the users video list as page content.I've noted that that the the part that i want is loaded by ajax upon click and the link on the address bar also chnages, means that i should be getting the video contents but its not the case.
<?php
set_time_limit(0);
function get_content_of_url($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
$plyst = get_content_of_url("http://vk.com/video56612186_167049188");
?>

The URL is sending a redirect , so add this cURL param to your existing thing..
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Related

Display image from curl request in html<img> tag

One of the pages of a website i'm working on should display information about a manga such as it's cover image.
I'm trying to display an image I got by making a curl request.
<?php
if(isset($_GET['info'])) {
$postedData = $_GET["info"]; //json object containing info about manga such as author/title etc.
$info = json_decode($postedData, true);
$mangacoverid = $info['im']; //id of the cover image that i'm getting from json object
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture; //displays image in browser
}
?>
-Some 'mangacoverid' for testing purposes:
ff/ff94bb880357b6b811bccbbfd3356c5ec41fbb184291323f0ed6a86a.jpg
c1/c1b0173d8986681f23ecf5a69d26aa9dab4a04db4d40f99bed539198.jpg
0c/0cf6ebf78074e748ab2aeea3a0fcb9e0dd43040974c66e24fa46703f.jpg
5d/5dcfed2e033c2da62e7ac89367533ebbc344373c46a005e274a16785.png
18/18b1f0b13bccb594c6daf296c1f9b6dbd83783bb5ae63fe1716c9091.jpg
35/35bf6095212da882f5d2122fd547800ed993c58956ec39b5a2d52ad4.jpg
-While I am able to display the image in the page, the whole page background becomes black with the image in the middle of the page. (i.e. style="margin: 0px; background: #0e0e0e;>").
What I am trying to do is to insert the image in an HTML tag, so that I can place it somewhere else in the page.
I tried just putting the normal cdn link with 'mangacoverid' attached to it in an tag but the image provider doesn't allow hotlinking and it throws 403 error.
Any help really appreciated!
I tested your code and it seems to be working correctly when the cover ID is hard coded.
Could the issue be that the JSON passed via query param is not getting parsed correctly (due to URL encoding)?
Does it work correctly for you with a cover ID hard coded?
File image.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf('https://cdn.mangaeden.com/mangasimg/%s', $_GET['id']));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture;
And in your script that generates the page displaying the image:
// Assuming $info holds the decoded json for the manga
echo(sprintf('<img src="image.php?id=%s>', $info['im']);
Ok, I'll post the solution in case anyone ever bumps into this.
The code is the mostly the same as original: just edited a few lines as shown below.
-I created a file in the directory called 'tempcover.jpg'.
-I got the image using the function 'imagecreatefromstring($picture)'.
-I saved the image into the file with 'imagejpeg($img,'tempcover.jpg', 100)'.
By doing this I can just refer to the file in the HTML tag and display it the way I want to.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
$img = imagecreatefromstring($picture);
imagejpeg($img,'tempcover.jpg', 100);
?>
<!--HTML-->
<img src="tempcover.jpg" alt="bruh" height="400px" width="300px">

Open a URL with a PHP within it using cURL

I am trying to fetch data from a URL using cURL...this is something I would be able to do but the URL that I want to fetch data from has a section of php code within it - i.e. the URL is populated using a values generated by an earlier section of code.
I want to access the URL, take the figures out of it and then plug those figures into something else. At the minute I am hitting a bit of a roadblock as the php doesn't populate the URL...any ideas?
The code I am using is:
$url = "https://*START OF API ADDRESS*<?php echo $request_ids; ?>*END OF API ADDRESS*";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
//var_dump(json_decode($result, true));
$rates = json_decode($result, true);
// Save result
$rates['last_updated'] = time();
file_put_contents($file, json_encode($rates));
}

using external API in php: Links are incorrect

I'm new to using and API in php.
I have an existing call to an external API, which works and looks like this:
// $id = "227";
// $url = "external-api.com/places/$id?;
// url above works, too
$url = "external-api.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$partner_code:$secret_code");
$result = curl_exec($ch);
curl_close($ch);
echo($result);
On my local server the page is displayed correctly when I enter http://localhost/mytest/mytest.php
The page also displays links and when I click on of them, it shows me the page could not be found. This is because the target is localhost/places/227 and there is no such page.
What I want now is that a call to localhost/places/227 shows a result of an API call to external-api.com/places/227. Just like in the example above, except that this time $url has changed to external-api.com/places/227.
But this shouldn't happen to every link on my website. Just to those links that were retrieved by the API call.
So I think this is what I want: When the link to /places/227 is clicked I want the above php script to be executed again except that the varaible for $url has changed to $url = "external-api.com/places/227;

Why the header function is not working after CURL call?

Following is the call to an URL using CURL :
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
$link = $_GET['link'];
$url = "http://www.complexknot.com/user/verify/link_".$link."/";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
The variable $url contains one URL which I'm hitting using CURL.
The logic written in the file(present in a variable $url) is working absolutely fine.
After executing the code I want the control to be redirected to one URL. For it I've written following code :
header('Location: http://www.complexknot.com/login.php');
exit;
The following code is not working. The URL http://www.complexknot.com/login.php is not opening and a blank white page appears. This is the issue I'm facing.
If I don't use the CURL and hit the URL i.e. the URL contained in $url then it gets redirect to the URL http://www.complexknot.com/login.php that means header function works fine when I hit the URL in browser.
Why it's not working when I call it from CURL?
Please someone help me.
Thanks in advance.
This is happening because CURL is outputting the data. You must use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); in order to let CURL returning data instead of outputting it.
<?php
ini_set('display_startup_errors', 0);
ini_set('display_errors', 0);
error_reporting(0);
$link = $_GET['link'];
$url = "http://www.complexknot.com/user/verify/link_$link/";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
header('Location: http://www.complexknot.com/login.php');
You can use
<?php echo "<script>window.location.href = 'http://www.complexknot.com/login.php';</script>";die; ?>

Get last available URL

I am using an API to get the users profile picture. The call looks something like this
https://familysearch.org/platform/tree/persons/{$rid}/portrait?access_token={$_SESSION['fs-session']}&default=https://eternalreminder.com/dev/graphics/{$default_gender}_default.svg
This link only works for about an hour because the user's session token expires then. I was wondering if there was any way to retrieve the last returned returned URL, which would be the direct link to the image, so I could store that in a database.
I have tried Google but I don't really know where to start.
Thanks in advance!
I was able to solve my own problem. It was doing a redirect to get the image and I just needed that URL. Here is my code that helped me get there.
$url="http://libero-news.it.feedsportal.com/c/34068/f/618095/s/2e34796f/l/0L0Sliberoquotidiano0Bit0Cnews0C12735670CI0Esaggi0Eper0Ele0Eriforme0Ecostituzionali0EChiaccherano0Ee0Eascoltano0Bhtml/story01.htm";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch); // $a will contain all headers
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL
// Uncomment to see all headers
/*
echo "<pre>";
print_r($a);echo"<br>";
echo "</pre>";
*/
echo $url; // Voila

Categories