I have two domains sharing the same server. One of them, site_1.com, holding images at directory /images and the other one site, site_2.com, must to show those images. I have the following code at site_2:
<html>
<body>
<img src="http://site_1.com/images/img.png" />
</body>
</html>
Result: It didn't work. After reading a lot of answers and comment here and other forums I applied their sugges of using for example an api at site_1 like this:
/api/get_image.php at site_1.com:
<?php
$file_name = $_GET['file_name'];
$url = 'http://site_1.com/images/'.$file_name;
header('Content-type: image/png');
imagepng(imagecreatefrompng($url));
?>
And call the api at site_2.com like this:
<html>
<body>
<img src='http://site_1.com/api/get_image.php?file_name='img.png' />
</body>
</html>
It didn't work.
Another way using file_get_contens():
<?php
$file_name = $_GET['file_name'];
$url = 'http://site_1.com/images/'.$file_name;
$img = file_get_contents($url) ;
echo $img ;
?>
It didn't work.
Another way using CURL library:
<?php
$file_name = $_GET['file_name'];
$url = 'http://site_1.com/images/'.$file_name;
//
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
echo $image ;
?>
It didn't work.
It's problably that I'm doing something wrong but I don't know what is. By the way, allow_url_fopen is ON and may be I have to active some other flag in some configuration file of apache or php. Please, could anybody tell me where is the problem? I appreciate any help.
sharing the same server.
Put images to some shared folder (i.e. both users for site_1 and site_2 can have access to it) and create symlinks to public folders of both sites
If you want to use your solution I think you should send appropriate header, e.g. header('Content-type: image/jpg');
header('Content-Type: image/'.$fileType);
readfile($filePath);
Related
I'm unable to scrape data from few websites using curls.
I'm using CURL to scrape website from url's. It works great in 80% of the urls I use. But some url's don't seem "scrapeable". For example, when I try to scrape https://www.nextdoorhub.com/ and https://www.atknsn.com/, it doesn't work. the website keeps showing blanks and at the end it doesn't return a result.
This is my code:
<center>
<br/>
<form method="post" name="scrap_form" id="scrap_form" action="scrape_data.php">
<b>Enter Website URL To Scrape Data:</b>
<input type="input" name="website_url" id="website_url">
<input type="submit" name="submit" value="Submit" >
</form>
</center>
<?php
error_reporting(E_ALL ^ E_NOTICE );
$website_url = $_POST['website_url'];
$result = scrapeWebsiteData($website_url);
function scrapeWebsiteData($website_url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $website_url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_BINARYTRANSFER,1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$regextit = '<div id="case_textlist">(.*?)<\/div>/s';
preg_match_all($regextit, $result, $list);
/* echo "<pre>";
print_r($list[1]); die; */
$regex = '/[\'" >\t^]([^\'" \n\r\t]+\.(jpe?g|bmp|gif|png))[\'" <\n\r\t]/i';
preg_match_all($regex, $result, $url_matches);
$count = count($url_matches[1]);
// set the local path of image
$local_path = 'C:\udeytech\htdocs\tests\images\\';
for($i=0; $i<$count; $i++)
{
preg_match_all('!.*?/!', $url_matches[1][$i], $matches);
$last_part = end($matches[0]);
////match image name last part of anything .jpg|jpeg|gif|png
preg_match("!$last_part(.*?.(jpg|jpeg|gif|png))!", $url_matches[1][$i], $matche);
$secons_part = $matche[0];
$info = pathinfo($secons_part);
$image_name = $info['basename'];
//save image url in a variable
$image_url = $url_matches[1][$i];
$image_path = scrapeWebsiteData($image_url);
$file_open = fopen($local_path.$image_name, 'w');
fwrite($file_open, $image_path);
fclose($file_open);
}
?>
Have you tried to load either of these sites in your browser and look at the responses?
nextdoorhub is using angular and atknsn looks to be heavy on jQuery. Long story short, these sites need to run javascript to render the full HTML you're intending to scrape.
Using PHP + cURL alone won't cut it. Look at threads that discuss scraping angular and that will point you in the right direction. (Hint: you need to scrape these sites with node.js)
I'm trying to grab an svg file with php and output it onto a page. Site is built on Wordpress.
<?php
echo file_get_contents("site.com/wp-content/themes/oaklandcentral/img/fb_logo.svg");
?>
Nothing is displaying. The actual link is correct and goes directly to the svg. Any thoughts?
Path is wrong, use file_get_contents( get_template_directory() . '/img/fb_logo.svg' )
echo file_get_contents("site.com/wp-content/themes/oaklandcentral/img/fb_logo.svg");
Assumptions:
your "filename" string starts with the protocol e.g. "http://"
that you can access the file directly from a browser (i.e. not a
file/folder permissions issue);
that file_get_contents is returning FALSE? (does the E_WARNING tell
you anything?)
I recollect seeing that some servers are not configured to allow file_get_contents (or readfile with URLs) so I would check investigate this first (maybe allow_url_fopen in php.ini???).
If this is not the case (or is a limitation by host provider) and you cannot find the cause of the problem then CURL should work (on most servers!).
$url = 'http://example.com/path-to/fb_logo.svg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
$svg = curl_exec($ch);
curl_close($ch);
echo $svg;
Edit:
It also appears you could also use php include if you remove the xml header tag from your SVG file.
I assume you want to display the SVG image not its text code, in which case why not simply <img src="<?php echo $mySvgFileUrl"; ?>"> like any other image?
Add header using: header("Content-Type: image/svg+xml"); before you echo the svg. sometimes the browser assumes the data received is html by default.
What could possibly be the problem.
The file isn't where you think it is.
File permission isn't allowing the code to read the file.
Try to debug:
<?php
$file = "site.com/wp-content/themes/oaklandcentral/img/fb_logo.svg";
if ( file_exists($file) ) {
echo file_get_contents($file);
} else {
echo "File $file does not exist";
}
?>
Try to get the page with curl. Take a look at the headers and the raw https response.
curl -v URL_TO_PAGE
Try:
<?php echo file_get_contents(get_template_directory().'/theme/img/chevron-right-solid.svg'); ?>
this seemed to work for me, where using get_template_directory_uri() did not (it threw an OpenSSL error)
Then, you can target the SVG and style how you wish.
For example:
<button class="btn btn-primary">
<?php echo file_get_contents(get_template_directory().'/theme/img/chevron-right-solid.svg'); ?>
</button>
CSS might be:
.btn svg { line-height: 1.2; max-height: 1rem;}
Try to use below code.
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
$file = file_get_contents('site.com/wp-content/themes/oaklandcentral/img/fb_logo.svg', false, $context);
How can i show an image from a server with standard http protection without showing the authentication window?
I now use standard html
<img src="...">
but because the image is protected this asks for an authentication window. I do have the login data, how can i show the image?
Regards, Tom.
This should work. Simply replace the username and password with your authentication details. (Warning: Doesn't work in all browsers)
<img src="http://username:password#server/Path" />
I would recommend putting this in a separate file on your server. That way you can reference it without exposing the authentication info.
I used IrishGeeks tip to get a solution. It works on all browsers. The script.php is
<?php
$url = $_GET['url'];
$c = curl_init($url);
$authString = 'user:pass';
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_USERPWD, $authString);
$content = curl_exec($c);
$contentType = curl_getinfo($c, CURLINFO_CONTENT_TYPE);
header('Content-Type:'.$contentType);
print $content;
?>
Then use
<?php
print '<img src="script.php?url='.urlencode('http://www.example.com/image.bmp').'" />';
?>
To get the image.
I would like to have code for an image that counts the number of times the image is viewed, regardless of what site the image is found on. I want to use the img src tag, and have src point to a php page that counts that view and then returns the image to be viewed. I was thinking something like this:
<img src="www.mywebsiteurl.com/something.php" />
How would I go about writing "something.php" so that it returns the proper image file? I know how to record the page view, but if I must do something different in this case, please tell me.
Your script needs to:
Record the hit.
Set the content type of the output ( header('Content-Type: image/jpeg'); )
Output the image ( readfile( $path_to_image ) );
For examples, please see the readfile documentation and Output an Image in PHP.
Loading images / files from php is slow than normal loading files.
Main cons:
Images can't be cached due to dynamic query most browser dont support on dynamic queries.
Overhead on sever and apache both.
If you use database and want to increment column after image loads than a person can load your image again and again. So you need to use conditions on every query.
Alternative Solution
Enter this code in footer of page on which you want to show images
A ajax call
$.get("inc_img_view.php?img_id="+<?php echo $img_id; ?>);
This is the most barebones version you can have:
<?php
header('Content-type: image/jpeg');
readfile('somepic.jpg');
You can call an image using a URL similar to: www.mywebsiteurl.com/image_renderer.php?url=url_to_image.
image_rendered.php:
<?php
header("Content-Type: image/jpeg");
$url = urldecode($_GET['url']);
$headers = array(
"GET ".$url." HTTP/1.1",
"Content-Type: text/xml; charset=UTF-8",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache"
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$r = curl_exec($ch);
// you can connect to your database and increment the number of times this image was viewed
echo $r;
?>
Hope this will point you into the right direction.
Yes you're on a good start.
I'd do something like this
<img src="www.mywebsiteurl.com/image_counter.php?src=image.jpg" />
In image_counter.php, take $src = $_GET['src'] for image source.
Then +1 the hit for that image in the database.
Then do
header('Content-type: image/jpeg');
readfile($src);
Something like this should work:
<?php
$file = $_GET['img'];
$path = "images/";
// do an increment of views on the image here - probably in a database?
$exifImageType = exif_imagetype($path.$file);
if ($exifImageType !== false) {
$type = image_type_to_mime_type($exifImageType);
}
header('Content-Type: $type');
echo file_get_contents($path.$file);
Note: this would make something like:
<img src="getimage.php?img=image.jpg" />
show the correct image after incrementing the views.
You could always add some .htaccess to make all calls to /images/anything go through your image file. It'd mean you could keep the URL looking as though it's getting an actual image file (e.g. /images/logo.jpg would be re-written to actually go through getimage.php?img=logo.jpg)
is this possible ?
what is the correct way to send files ?
thanks
I don't if you want your webservice to upload/download files. Anyway you can use curl(http://fr.php.net/curl ) to upload/download file from other webserver.
To get some file uploaded to the webservice from the user it's pretty much the same as gettings it from a form, please use the superglobal variable:$_FILES (doc) to get upload files.
for uploading from php to a webservice
$fullflepath = 'C:\temp\test.jpg';
$upload_url = 'http://www.example.com/uploadtarget.php';
$params = array(
'photo'=>"#$fullfilepath",
'title'=>$title
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
curl_close($ch);
the webservice to get a file
$uploads_dir = '/uploads';
foreach ($_FILES["photo"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["photo"]["tmp_name"][$key];
$name = $_FILES["photo"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
PS: sorry for some reason stackoverflow doesn't like to make a link of $_FILES ... so I have linked the superglobals page instead
You use this php program based on nusoap : http://biclim.com/WSClients.action
You can debug the response of your php service and check the file upload from iphone using this app - http://itunes.apple.com/us/app/rest-client/id503860664?ls=1&mt=8
It was really helpful to debug serverside code.
Hello Here Example Image upload
<?php
$path="aa/";// Set your path to image upload
if(!is_dir($path)){
mkdir($path);
}
$roomPhotoList = $_POST['image'];
$random_digit=date('Y_m_d_h_i_s');
$filename=$random_digit.'.jpg';
$decoded=base64_decode($roomPhotoList);
file_put_contents($path.$filename,$decoded);
?>
it can be quick image upload code in php for ios and android