curl Authentication error on Images Only


curl Authentication error on Images Only



I am trying to get information from a XML Rest API.



I can get everything but not the images.



I can display all info, but when it comes on images I get 401 error



Failed to load resource: the server responded with a status of 401 ().


Failed to load resource: the server responded with a status of 401 ()



My username and password are correct. The only way to get the images display is if I login to this API in a different window. Then all images are displayed.



Am I doing something wrong ? Here is my php code:


function CallAPI($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "usr:psw");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}

$a = "query SQL";
$asd = $database->query($a);
while($row = sqlsrv_fetch_array($asd)){
echo $url = $row['url']; // I can get this data display
$row['hotel_name'];
$a = CallAPI($url);
$axml = new SimpleXMLElement($a);
$img_link = $axml->item->images;
foreach ($img_link->image as $value) {
echo "<img src='".$value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink')."' class='img-responsive' />";
}





In the line $a = CallAPI($url); your backend server reads the data. But when it comes to images, you are only generating IMG tags. That means that the server is not fetching the images, but a webbrowser. And probably the browser does not have permissions to fetch those images.
– JustAC0der
Jun 21 at 11:03



$a = CallAPI($url);





@JustAC0der if I open a new window and login in that api I can display the images. But when send my user-pass through curl i can access everything in API except of images
– Maria
Jun 21 at 11:06





I am guessing, but it seems that you need two PHP scripts: one for fetching the main data (from $row['url']) and another one for fetching images (let's say we call it fetch_images.php). And you need to link from the first one to the second one, eg. '<img src="/fetch_images.php?url=' . urlencode($value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink')) . '">').
– JustAC0der
Jun 21 at 11:19


$row['url']


fetch_images.php


'<img src="/fetch_images.php?url=' . urlencode($value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink')) . '">'




1 Answer
1



So your problem must be that this link need authentication as well. So try make the call for $value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink') to pass user and pass.


$value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink')



So you code should look like this


while($row = sqlsrv_fetch_array($asd)){
$url = $row['url'];
$row['hotel_name']; // No use of this one, maybe you use it on your end
$a = CallAPI($url);
$axml = new SimpleXMLElement($a);
$img_link = $axml->item->images;
foreach ($img_link->image as $value) {
/*I am adding the link to a variable*/
$imgLinkFromValue = $value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink');
$img = CallAPI($imgLinkFromValue );
/* Now if you still get a link just pass the variable */
echo "<img src='$img->**link or href or anything you get**' class='img-responsive' />";
}



UPDATE



If you getting text or symbols maybe you have to use base64_encode().


base64_encode()



Try this :




$img = base64_encode(CallAPI($imgLinkFromValue ));
echo '<img src="data:image/jpeg;base64,' . $img . '" class="img-responsive" />'



$img = base64_encode(CallAPI($imgLinkFromValue ));
echo '<img src="data:image/jpeg;base64,' . $img . '" class="img-responsive" />'



You can always change the data inside src to png or gif, depends on what you need.



For more Info about base64_encode function you can use this link.
http://php.net/manual/en/function.base64-encode.php





Thanks for you answer but if I use CallAPI I cant get the image I get a wierd string and some symbols instead of the image
– Maria
Jun 29 at 11:00





@Maria I have updated my answer. Hope it helps :)
– DopeAt
Jun 29 at 11:06





Thank you so much it worked!!!!
– Maria
Jun 29 at 11:23






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV