I am using PHP script (currentimage.php) to get a snapshot from my CCTV IP camera, which works fine:
<?php
while (#ob_end_clean());
header('Content-type: image/jpeg');
// create curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL, 'http://192.168.0.20/Streaming/channels/1/picture');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
// $output contains the output string
$output = curl_exec($ch);
echo $output;
// close curl resource to free up system resources
curl_close($ch);
?>
and another PHP/HTML to display the data:
<IMG id="myImage" SRC='currentimage.php'>
but I can't get it to work to refresh the image in background every 30s.
I am trying AJAX, but no success:
<script>
function refresh_image(){
document.getElementbyId("myImage").setAttribute("src", "currentimage.php");
}
setInterval(function(){refresh_image()}, 30000);
</script>
What I am doing wrong? I will appreciate any help
I have tried the concept of changing the displayed image via timed requests to a PHP script, and it works fine. A new request is fired up to the image getting script if the URL is modified, and I did so by appending a version number as a parameter to the URL.
var version = 1;
function refresh_image(){
document.getElementById("myImage").setAttribute("src", "currentimage.php?ver="+version);
version++;
}
setInterval(function(){
refresh_image();
}, 2000);
I suspect the main problem here is that the browser caches your file and will not reload it if you set the attribute again. I've found a way around this though:
document.getElementbyId("myImage").setAttribute("src", "currentimage.php?version=1");
Save the version number and count up every time you make the request. This way, the browser will treat it as a new URL and it will be reloaded again (check this using the developer functions of your browser in the network tab).
Related
My little application works in this way: in a simple text input user are asked to insert an url. on change my script tries to extract and display the first 10 images found on this page.
<input type="text" name="url" id="url" value="">
$("form.link-form input#url").change(function() {
var request = $.ajax({
type: "GET",
url: "funzioni/ajax/loadImagestFromUrl.php",
data: "url=" + insertedUrl,
dataType: "html",
timeout: 5000,
success: function(res) {
loadUrlImages2div(msg99);
},
error: function() {
request.abort();
}
});
});
the PHP script loadImagestFromUrl.php run this code, using PHP Simple HTML DOM Parser library:
set_time_limit(5);
$html = file_get_html($url); // load into this variable the entire html of the page
$count=1;
foreach($html->find('img') as $key=>$element) { // only images
if ($count==11) break; //only first 10 images
echo "<img class=\"imgFromUrl\" src=\"".$element->src."\" />\n";
}
$count++;
}
This work great on most cases but there are some urls that are not available in few seconds, or protected under password and the server keep executing something even if I set a timeout of 5 seconds for the ajax request and 5 second for the execution of php code.
When this happens everything get blocked, even refreshing the page is impossible, because it load and load and only after a lot of time it returns "504 Gateway Time-out. The server didn't respond in time."
Can somene help me in understanding how to completely block this request and let the server keep working?
Got that the solution must be find in the php code timeout and not with ajax timeout. This is clear.
I found this interesting discussion
Great answer: Handling delays when retrieving files from remote server in PHP
that suggests to use context parameter in the file_get_contents function.
But actually it doesn’t work on my app.
So, as suggested by Wieger I have tried to use curl instead of file_get content. I defined this function
function file_get_contents_curl($url) {
$ch = curl_init();
$timeout=2;
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
and used it instead of file_get_contents inside the parser library.
Again, it works for most of case, but some url make the server load for a lot of time until gateway timeout.
I am trying to crawl through a page but only loading GIF is retrieved not the page content.
$url = "https://www.truecaller.com";
$request = $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
print_r($data);
curl_close($ch);
any way to retrieve full page.
There is a reason for that.
Curl is not the browser.so Curl does not have the ability to run javascript.
Curl Does not care what is the response it gets you whatever you give it link for. if it gets a gif it will return gif, it that's doc, video or whatever it will return the response.
so what's happening is that It gets your response as soon as you hit the page. there is a gif that is being loaded at first it will return you loading gif. then on the base of javascript condition remaining page is loaded. as it fails to execute javascript so the only response you are getting is that loading gif.
If you want to load full page content there is a full webkit browser without an interface that helps programmers to achieve results as a browser gets.PhantomJS - Scriptable Headless Browser.
I see you have already tried adding a delay to your curl, but the fact is curl is not the right tool for this job. I would investigate http://phantomjs.org/ which will allow you to capture the page more robustly.
#hassan added below, this site has an API so that is also an option. Thanks hassan.
I have a IP CCTV camera which support MJPEG. I would like to display image in HTML (from PHP script) together with other data.
I know that I can get a screenshot from the camera using:
http://username:password#<servername>/Streaming/channels/1/picture
however when I am using this simple html code image is not always displayed:
<html>
<body>
<IMG id="myImage" SRC='http://username:password#192.168.0.20/Streaming/channels/1/picture'>
</body>
</html>
On Microsoft Edge, it is not possible even to login to camera.
On Firefox works fine.
On Chrome camera is logged however image is not displayed.
So the question is how I can get image in HTML or better in PHP to display it on the page. I prefer to use PHP because I want to add more data to page, like temperature etc.
Also will be nice to refresh the image, but this can be done in AJAX later.
Use a second PHP script to retrieve the image and point the <img> tag to that. It's possibly because the page you're displaying the image on uses HTTPS while the camera only supports HTTP. Proxying through PHP will allow it all to appear to come from the same source and PHP will handle the HTTP authentication, which will avoid any of your browser compatibility problems.
<IMG id="myImage" SRC='currentimage.php'>
currentimage.php:
<?php
$crl = curl_init('http://<servername>/Streaming/channels/1/picture');
curl_setopt($crl, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($crl, CURLOPT_USERPWD, 'username:password');
curl_setopt($crl, CURLOPT_RETURNTRANSFER, TRUE);
$contents = curl_exec($crl);
curl_close($crl);
header("Content-Type: image/jpeg");
echo $contents;
?>
To save on server load and allow it to handle more traffic, you could also cache the last-retrieved image to a file for 5-10 seconds, but that's a separate problem to solve.
This code seems to work fine for me:
<?php
while (#ob_end_clean());
header('Content-type: image/jpeg');
// create curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL, 'http://<servername>/Streaming/channels/1/picture');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
// $output contains the output string
$output = curl_exec($ch);
echo $output;
// close curl resource to free up system resources
curl_close($ch);
?>
I am visiting one site, which already provide following functionality: When user browser visits this url, it will automatically prompt window to download containing file (video). In my script I need to download this attached file from this site to disk using php. I tried to use curl:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
And also similar file_get_contents but it doesnt work for me. It get me only containing HTML on that site. Have you any idea how to save this file on the disk using php?
One standard way that developers do this in PHP is to use the "header" method. After calling header, the page can output the video contents, which will prompt a user download.
See this example: PHP header attach AVI-file
http://bulksms.poweredsms.com/send.php?usr=rajdeeps&pwd=pass123&ph=xxxxxxxxxxx&sndr=textid&text=hi
I need to open this url, but there is no way I can redirect back to my own website from this link, is there any way to execute this link & den carry on with other webpages I want...??
You can use CURL for this.
http://en.wikipedia.org/wiki/CURL
I don't know what you mean with "executing" a url, if you just want to call it, you can use AJAX, open an popup or just do something like this (very ugly method):
<img src="url....." style="display: none;">
Edit:
One example of using AJAX is here:
Get result from php file without usig jquery
You could try this way using cURL
<?php
$ch = curl_init("http://bulksms.poweredsms.com/send.php?usr=rajdeeps&pwd=pass123&ph=xxxxxxxxxxx&sndr=textid&text=hi");
curl_setopt($ch, CURLOPT_URL, $ch);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_exec($ch);
curl_close($ch);
?>