-1

This screenshot shows that the URL is getting stored in $url

Image

This screenshots shows that after I add echo $html to the code, it says undefined variable $url and file_get_contents(): filename cannot be empty

Image2

Also, I have tried almost everything that's there on stackoverflow including file_get_html() and cURL. Nothing seems to work. Please tell me where I'm going wrong here.

<?php
include_once('simple_html_dom.php');
$base_url = "https://www.instagram.com/";
$html = "";
if ( isset($_POST['username']) ) {
    $url = $base_url.htmlspecialchars($_POST['username'])."/";//concatenate $base_url to username to generate full URL
}
$html = file_get_contents($url); //access the URL in $url
$doc = new DOMDocument;
$doc->loadHTML($html); //get HTML of the webpage given by file_get_contents
$tags = $doc->getElementsByTagName('img');
$arr = (array)$tags;
if (empty($arr)) {
    echo 'emptyarray';
}
foreach ($tags as $tag) {
    echo $tag->getAttribute('src');
}
?>

Edit:

If 'http:// stackoverflow.com/questions' is used instead of 'https:// www.instagram.com/ its_kushal_here' file_get_contents() is working fine and not failing.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • `if ( isset($_POST['username']) ) { $url = $base_url.htmlspecialchars($_POST['username'])."/"; }` This segment is not successful means your post was not successful – M A SIDDIQUI Feb 07 '17 at 10:12
  • 1 - Remove all testing code : 'echo $url...' There is a lot of testing code that makes it harder to see what is happening----- 2 - Include 'simple_dom_html' file ------ 3 - Try include the code snippets with codepen (http://codepen.io/)---- 4 - Consider using comments to show what you are trying to do – Tim Cason Feb 07 '17 at 10:18
  • @MASIDDIQUI Even I thought so. But, the first screenshot will show you that the POST is successful. – KUSHAL Kale Feb 07 '17 at 10:33
  • @Cason Noted and acted upon. Thank you. Let me know if you figure out whats wrong. – KUSHAL Kale Feb 07 '17 at 10:39
  • So its easy .. go to instagram.com/its_kushal_here/ view source code and find img tag... do you ??? – M A SIDDIQUI Feb 07 '17 at 10:41
  • @MASIDDIQUI yes.. img tag is there and can be viewed from the browser by visiting the url and clicking 'view source'. I need to access it using my PHP script. – KUSHAL Kale Feb 07 '17 at 10:45
  • See Chapter Notes: http://php.net/manual/en/function.file-get-contents.php. The error message says html does not contain anything, so file_get_contents failed. – radicarl Feb 07 '17 at 10:46
  • i don't see any ... img tag – M A SIDDIQUI Feb 07 '17 at 10:48
  • @radicarl I read the manual. file_get_contents has failed, yes. Unable to find out why though. Already read almost everything there is. On failure it should return FALSE. It is not. – KUSHAL Kale Feb 07 '17 at 10:52
  • did you check your php.ini settings? echo phpinfo(); allow_url_fopen must be 1 – radicarl Feb 07 '17 at 10:55
  • @radicarl both local value and master value for allow_url_fopen is 'On' should i change it to '1'? – KUSHAL Kale Feb 07 '17 at 11:01
  • @MASIDDIQUI I am certain. img tag is present on https://www.instagram.com/its_kushal_here – KUSHAL Kale Feb 07 '17 at 11:02
  • If you look at the source code of the site, there are no img-tags. Save the page and look at it with a text editor. There will be no img-tags. The img-tags you see, are created with javascript. – radicarl Feb 07 '17 at 12:02
  • @radicarl I get it now. Can you recommend a workaround or a solution for the problem? Thank you in advance. – KUSHAL Kale Feb 07 '17 at 12:05
  • there is a js object in the source code: window._shared, this contains a "display_src" for every pic. i would use a regex to extract the images from the js string – radicarl Feb 07 '17 at 12:13
  • @radicarl I will try doing that. one thing though. I saved the webpage and then opened it using brackets text editor. it has img tags. how is this possible then? – KUSHAL Kale Feb 07 '17 at 12:30
  • then your browser did not saved the page it got from the server. it saved the page after running all the js on the page. Firefox offers both options (something like save whole page and save only html). – radicarl Feb 08 '17 at 10:40

1 Answers1

-1

When you refreshed the page did you make sure your post parameters carried through to the new request ?

The issue seems to be here

if ( isset($_POST['username']) ) {
    $url = $base_url.htmlspecialchars($_POST['username'])."/";
}

If $_POST['username'] is not set then $url will not be defined. Also remove the @ from @$doc->loadHTML($html); so you can see the error it outputs. That will help you workout what fails after that point.

  • Kirsty Wright. Yes I made sure that the POST parameters were set. I didn't refresh the page. Instead, I filled out the form on the previous page again so that all the POST params get their values. AND this is what i get extra after removing the '@'. Warning: DOMDocument::loadHTML(): Empty string supplied as input in C:\xampp\htdocs\instaDP\php\process.php on line 12 – KUSHAL Kale Feb 07 '17 at 10:20