quasiman Posted March 4, 2010 Share Posted March 4, 2010 Hello all, I'm reading an XML file for advertising on my site, and the image urls are sometimes not real images (jpg,gif,png). For instance the url can be like this: http://www.website/get_image.aspx?domain=website.com&image_guid=e17095e8-c303-4ff0-8767-fada7575ff16&size=1 Here's the curl I'm using, and I need to change it to get a real file image saved... $filepath = $result->imageurl; $path = explode('/', $filepath); $p=$path[count($path)-1]; //curl to get image $ch = curl_init($filepath); $fp = fopen($p,'w'); curl_setopt($ch, CURLOPT_TIMEOUT, 50); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_exec($ch); curl_close($ch); fclose($fp); Exploding the filepath doesn't work for this kind of image link, but I'm not sure how else to get it. Thanks for any help! Link to comment https://forums.phpfreaks.com/topic/194156-curl-to-download-image-through-xml-feed-not-an-image-file/ Share on other sites More sharing options...
teamatomic Posted March 4, 2010 Share Posted March 4, 2010 I'm not sure here. Is the link example you showed contained within an xml file returned by a curl_exec? HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/194156-curl-to-download-image-through-xml-feed-not-an-image-file/#findComment-1021574 Share on other sites More sharing options...
quasiman Posted March 4, 2010 Author Share Posted March 4, 2010 Yes and no, I'm reading the xml with simplexml_load_file. Yes it's contained in the xml file, no it's not returned by curl. Here's more detail on what I'm doing.... Reading/Displaying the XML: $url = "products.xml"; $xml = simplexml_load_file($url); foreach ($xml->item as $result) { $filepath = $result->imageurl; $path = explode('/', $filepath); $p=$path[count($path)-1]; //curl to get image $ch = curl_init($filepath); $fp = fopen($p,'w'); curl_setopt($ch, CURLOPT_TIMEOUT, 50); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_exec($ch); curl_close($ch); fclose($fp); ?> <dl> <dt><a href="<?php echo $result->linkurl; ?>"><img src="<?php echo $result->linkimage; ?>" /></a> <a href="<?php echo $result->linkurl; ?>"><?php echo $result->productname; ?></a> </dt> <dd> <p><?php echo $result->description->short; ?></p> </dd> <dd> <p><?php echo $result->description->long; ?></p> </dd> </dl> <?php } ?> Actual XML file: <?xml version="1.0" encoding="UTF-8"?> <result> <TotalMatches>15</TotalMatches> <TotalPages>3</TotalPages> <PageNumber>1</PageNumber> <item> <mid>25095</mid> <merchantname>BikeBandit.com</merchantname> <linkid>174489</linkid> <createdon>2010-03-03/16:19:32</createdon> <sku>5385305-001</sku> <productname>Product name</productname> <category> <primary>Automotive</primary> <secondary>Tires and Wheels</secondary> </category> <price currency="USD">230.95</price> <upccode/> <description> <short>Computer-designed tread pattern with offset groove alignment allows for uniform wear characteristics</short> <long/> </description> <keywords/> <linkurl>http://click.linksynergy.com/fs-bin/click?id=/Pn1BiM4EPg&offerid=162766.174489&type=15&subid=0</linkurl> <imageurl>http://www.website/get_image.aspx?domain=website.com&image_guid=e17095e8-c303-4ff0-8767-fada7575ff16&size=1</imageurl> </item> </result> Link to comment https://forums.phpfreaks.com/topic/194156-curl-to-download-image-through-xml-feed-not-an-image-file/#findComment-1021683 Share on other sites More sharing options...
teamatomic Posted March 4, 2010 Share Posted March 4, 2010 http://www.website/get_image.aspx?domain=website.com&image_guid=e17095e8-c303-4ff0-8767-fada7575ff16&size=1 Is this the actual url thats in the xml file or did you just fudge it in there? HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/194156-curl-to-download-image-through-xml-feed-not-an-image-file/#findComment-1021702 Share on other sites More sharing options...
quasiman Posted March 4, 2010 Author Share Posted March 4, 2010 I just fudged it....I guess it doesn't hurt to give the real url: http://www.fanzz.com:80/mainstreet/get_image.aspx?domain=fanzz.com&image_guid=e17095e8-c303-4ff0-8767-fada7575ff16&size=1 Link to comment https://forums.phpfreaks.com/topic/194156-curl-to-download-image-through-xml-feed-not-an-image-file/#findComment-1021706 Share on other sites More sharing options...
teamatomic Posted March 4, 2010 Share Posted March 4, 2010 I put the img url in the xml file and I get the raw image put to screen, no problem. Which by your code is what should happen. I dont understand what you are trying to do. Putting the url into the curl_init blasts it directly to the screen $p has a value of the tail end of a url fopen of $p is bound to fail you fopen for a write yet there is no fwrite, writing is not automatic, and no matter cause curl_init($url) goes directly to screen. The image is a lakers Tshirt. What are you trying to do with the image? HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/194156-curl-to-download-image-through-xml-feed-not-an-image-file/#findComment-1021720 Share on other sites More sharing options...
quasiman Posted March 5, 2010 Author Share Posted March 5, 2010 Apparently by trying to make this less confusing, I've made it more so...sorry You're right, displaying the original image works fine, but I want to save the image to my server and use another function to create a thumbnail for it. So it's formatted to my website, and cached for later use. That's where I'm saying: $filepath = $result->imageurl; $path = explode('/', $filepath); $p=$path[count($path)-1]; variable $p should equal something like "image.jpg", which I can use for the thumbnail function. Link to comment https://forums.phpfreaks.com/topic/194156-curl-to-download-image-through-xml-feed-not-an-image-file/#findComment-1021725 Share on other sites More sharing options...
teamatomic Posted March 5, 2010 Share Posted March 5, 2010 I did it up a bit. <?php $p='lakersT.jpg'; $url = "./products.xml"; $xml = simplexml_load_file($url); foreach ($xml->item as $result) { $filepath = $result->imageurl; } //$path = explode('/', $filepath); //$p=$path[count($path)-1]; //echo "$p"; //exit; $fh=fopen("$p","w"); $ch = curl_init($filepath); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 50); curl_setopt($ch, CURLOPT_FILE, $fh); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_exec($ch); curl_close($ch); ?> <img src="lakersT.jpg"> [/url] This will write an image: lakersT.jpg and display it. There is no valid image name in $p to grab at all, no matter if you used a regex or whatever. You will have to come up with a way to do it on your own like I did at the start by giving $p a valid image. Now, the way I short stopped the foreach take for granted there is only one image in the xml file. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/194156-curl-to-download-image-through-xml-feed-not-an-image-file/#findComment-1021733 Share on other sites More sharing options...
quasiman Posted March 5, 2010 Author Share Posted March 5, 2010 I think this could cause problems though for a couple of reasons....if for instance the image is not actually a jpg type, and also if I have more than one image that doesn't go straight to a jpg,gif,png type image. I've found another method, that may work for me: <?php $url = 'http://www.fanzz.com/mainstreet/get_image.aspx?domain=fanzz.com&image_guid=e17095e8-c303-4ff0-8767-fada7575ff16&size=1'; $array = get_headers($url, 1); foreach( $array as $key => $value){ echo "$key, value: $value <br />"; } ?> I'll have to play with this and see where it takes me. Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/194156-curl-to-download-image-through-xml-feed-not-an-image-file/#findComment-1022030 Share on other sites More sharing options...
teamatomic Posted March 5, 2010 Share Posted March 5, 2010 If you look at the output of raw image code the first part is the header that has the format. For instance, jpeg is JFIF HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/194156-curl-to-download-image-through-xml-feed-not-an-image-file/#findComment-1022042 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.