Get Url Content PHP

Get file content from URL?

Depending on your PHP configuration, this may be a easy as using:

$jsonData = json_decode(file_get_contents('https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json'));

However, if allow_url_fopen isn't enabled on your system, you could read the data via CURL as follows:

<?php
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, 'https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json');
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);

$jsonData = json_decode(curl_exec($curlSession));
curl_close($curlSession);
?>

Incidentally, if you just want the raw JSON data, then simply remove the json_decode.

Get content from a url using php

You are fetching a JavaScript snippet that is supposed to be built in directly into the document, not queried by a script. The code inside is JavaScript.

You could pull out the code using a regular expression, but I would advise against it. First, it's probably not legal to do. Second, the format of the data they serve can change any time, breaking your script.

I think you should take at their RSS feed. You can parse that programmatically way easier than the JavaScript.

Check out this question on how to do that: Best way to parse RSS/Atom feeds with PHP

PHP Get URL Contents And Search For String

Just read the contents of the page as you would read a file. PHP does the connection stuff for you. Then just look for the string via regex or simple string comparison.

$url = 'http://my.url.com/';
$data = file_get_contents( $url );

if ( strpos( 'maybe baby love you', $data ) === false )
{

// do something

}

PHP Get Contents of a URL or Page

If you only need GET and allow_url_fopen is enabled on your server, you can simply use

$data = file_get_contents('http://api.somesite.com');

Get the full URL in PHP

Have a look at $_SERVER['REQUEST_URI'], i.e.

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

(Note that the double quoted string syntax is perfectly correct)

If you want to support both HTTP and HTTPS, you can use

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Editor's note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.

How to fetch the url content using php curl or file_get_content on the same server?

I think you have DNS resolving problem.

There is two ways you can use your website's locahost instead of the external domain name.

1. If you have your own server / VPS / Dedicated server,
Add entry in vim /etc/hosts for 127.0.0.1 website.net or try to fetch the content with localhost(127.0.0.1).

2. If you are using shared hosting, then try to use below url(s) in your code,

http://localhost.mywebsite.net/~username/home.php. 
OR
Try to call http://localhost.mywebsite.net/home.php

Try this to fetch the url content -

$dynamic = TRY_ABOVE_URL(S)
$out = "home.html" ;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$dynamic);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$file = curl_exec($ch);

if($file === false) {
echo 'Curl error: ' . curl_error($ch);
}

file_put_contents($out, $file);

How do I get the HTML code of a web page in PHP?

If your PHP server allows url fopen wrappers then the simplest way is:

$html = file_get_contents('https://stackoverflow.com/questions/ask');

If you need more control then you should look at the cURL functions:

$c = curl_init('https://stackoverflow.com/questions/ask');
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);


Related Topics



Leave a reply



Submit