2

I am trying to get the HTML of a URL on my own site. The site is built with laravel 5.4.

I am trying to get the HTML content of an endpoint so i can store it in the database as plain text.

but for some reason i keep getting continuous loading, even though i'm running on localhost.

this is what i've tried according to some questions (Can't seem to get a web page's contents via cURL - user agent and HTTP headers both set?) i've seen here on stack overflow:

$url = url("template/1/11"); // http://localhost:8000/template/1/11
$html = file_get_contents($url);

AND

$url = url("template/1/11"); // http://localhost:8000/template/1/11  
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)

$html = curl_exec($c);

if (curl_error($c))
    die(curl_error($c));

// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);

curl_close($c);

Is there a reason i'm getting this behavior? Please help i just need the HTML content of the URL

Raymond Ativie
  • 1,747
  • 2
  • 26
  • 50

2 Answers2

2

You have to specify the file's extension in your URL.

If your file is named index.html and is inside a folder called template, it's incorrect to use url as template/index, rather, you have to change it to template/index.html.

EDIT:

I had misread a part of the question. Apparently file_get_contents() doesn't work on Laravel routes, I suggest you to refer to this answer.

From the link:

$html = View::make($url)->render();

You might have to adjust the url if it's not pointing to the right route.

  • none of my URLs end with `.php` or `.html`. i am not pointing to a file but a PHP laravel route that when visited on the browser, shows a HTML page – Raymond Ativie Jun 22 '17 at 00:03
  • @RaymondAtivie Updated my answer. –  Jun 22 '17 at 00:07
  • This worked for me as expected. and like suggested, i had to tweak my `$url` to point to the view and pass my data with it `$html = View::make("template.page", $data)->render()` – Raymond Ativie Jun 22 '17 at 00:30
0

You are trying to access a laravel route which is fine, but the way you're doing it PHP will interpret as a local filename and the route won't be processed. You need to use the fully qualified domain name and path for the URL.

Matt
  • 5,315
  • 1
  • 30
  • 57
  • That is the full domain name path. the $url equals `"// http://localhost:8000/template/1/11"` i updated the question to reflect that – Raymond Ativie Jun 22 '17 at 00:01
  • You need to literally set that as your URL: `$url = url("http://localhost:8000/template/1/11");` – Matt Jun 22 '17 at 00:02
  • If you look in your php error log it's going to tell you that path doesn't exist because it is looking for public/template/1/11 which is not a file. It's a laravel route so you need to run it with the FQDN and port in front. – Matt Jun 22 '17 at 00:04