Get File Content from Url

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.

How can i get file url data in javascript

You want to use Blob url for shorter src.

Convert Base64 to Blob

Change:

reader.result

to:

window.URL.createObjectURL(file)

function previewFile() {
const preview = document.getElementById('video1');
const file = document.querySelector('input[type=file]').files[0];
const reader = new FileReader();

reader.addEventListener("load", function() {
// convert to base64
preview.src = window.URL.createObjectURL(file);
}, false);

if (file) {
reader.readAsDataURL(file);
}
}
<input type="file" onchange="previewFile()"><br>

<video width="320" height="240" id="video1" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>

Get contents of .txt file that is downloaded from url

You were almost there. The fetch method returns a promise which needs to be resolved to get the data. Using .then you can get the response from the fetch and then use the .json() method to extract the JSON from the server's response.

function translate(q) {

var sourceText = q;

var sourceLang = 'en';

var targetLang = 'es';

var url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + sourceLang + "&tl=" + targetLang + "&dt=t&q=" + encodeURI(sourceText);

var result = fetch(url).then((resp) => {

return resp.json();

});

return result; // As Promise

}

document.getElementById("fetch").addEventListener("click", () => {

let promise = translate(document.getElementById("source").value);

promise.then((json) => {

document.getElementById("result").innerHTML = JSON.stringify(json);

});

});
<html>

<body>

<input type="text" id="source"></input>

<button id="fetch">fetch</button>

<div id="result"></div>

</body>

</html>

Uploading a file from url

This simple code will download and save the image to a file

<?php
$url = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';

$file = file_get_contents($url);

if ($file != NULL) {
$pathToFolder = 'images/png/'; // just an example folder location
$fileName = $pathToFolder . md5(uniqid()) . 'png';
file_put_contents($fileName,$file);
} else {
echo 'File downlaod error';
}

Retrieve data from url and save in php

A couple of things,

http://website.com/data gets a 404 error, it doesn't exist.

Change your code to

$site = 'http://www.google.com';
$homepage = file_get_contents($site);
$filename = 'reviews.txt';
$handle = fopen($filename,"w");
fwrite($handle,$homepage);
echo "Success";
fclose($handle);

Remove $somecontent = echo $richSnippets; it doesn't do anything.

if you have the proper permissions it should work.

Be sure that your pointing to an existing webpage.

Edit

When cURL is enabled you can use the following function

function get_web_page( $url ){
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);

$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
curl_close( $ch );

return $content;
}

Now change

$homepage = file_get_contents($site);

in to

$homepage = get_web_page($site);

Fetch file content into string from url in Vue Js

There is nothing wrong with your code, you are using a promise chain to get the result of the fetch, but you're getting a CORS error on the request which isn't captured.

I have changed the format of the code and added an extra try catch you will see 'error in fetch' logged to the console.

async parseFile() {
try {
const fetchResponse = await fetch(
"https://filesamples.com/samples/document/txt/sample3.txt"
);
console.log(fetchResponse.text());
} catch (ex) {
console.log("Error in fetch");
}
}

https://codepen.io/DanielRivers/pen/qBVpZOo

How to get content from file from this URL?

var webRequest = WebRequest.Create(@"http://yourUrl");

using (var response = webRequest.GetResponse())
using(var content = response.GetResponseStream())
using(var reader = new StreamReader(content)){
var strContent = reader.ReadToEnd();
}

This will place the contents of the request into strContent.

Or as adrianbanks mentioned below simply use WebClient.DownloadString()

How to get a File() or Blob() from an URL in javascript?

Try using the fetch API. You can use it like so:

fetch('https://upload.wikimedia.org/wikipedia/commons/7/77/Delete_key1.jpg')
.then(res => res.blob()) // Gets the response and returns it as a blob
.then(blob => {
// Here's where you get access to the blob
// And you can use it for whatever you want
// Like calling ref().put(blob)

// Here, I use it to make an image appear on the page
let objectURL = URL.createObjectURL(blob);
let myImage = new Image();
myImage.src = objectURL;
document.getElementById('myImg').appendChild(myImage)
});
<div id="myImg"></div>


Related Topics



Leave a reply



Submit