仅图像上的 curl 身份验证错误
curl Authentication error on Images Only
我正在尝试从 XML 休息 API 获取信息。
我什么都可以,就是图片不行。
我可以显示所有信息,但是当它出现在图像上时我得到 401 错误
Failed to load resource: the server responded with a status of 401 ()
.
我的用户名和密码是正确的。显示图像的唯一方法是我在另一个 window 中登录此 API。然后显示所有图像。
我做错了什么吗?这是我的 php 代码:
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' />";
}
所以你的问题一定是这个 link 也需要身份验证。因此,请尝试调用 $value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink')
以传递用户并传递。
所以你的代码应该是这样的
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' />";
}
更新
如果你得到文本或符号,你可能必须使用 base64_encode()
。
试试这个:
$img = base64_encode(CallAPI($imgLinkFromValue ));
echo '<img src="data:image/jpeg;base64,' . $img . '" class="img-responsive" />'
您可以随时将 src 中的数据更改为 png 或 gif,具体取决于您的需要。
有关 base64_encode 函数的更多信息,您可以使用此 link。
http://php.net/manual/en/function.base64-encode.php
我正在尝试从 XML 休息 API 获取信息。
我什么都可以,就是图片不行。
我可以显示所有信息,但是当它出现在图像上时我得到 401 错误
Failed to load resource: the server responded with a status of 401 ()
.
我的用户名和密码是正确的。显示图像的唯一方法是我在另一个 window 中登录此 API。然后显示所有图像。
我做错了什么吗?这是我的 php 代码:
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' />";
}
所以你的问题一定是这个 link 也需要身份验证。因此,请尝试调用 $value->sizes->size[1]->attributes('http://www.w3.org/1999/xlink')
以传递用户并传递。
所以你的代码应该是这样的
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' />";
}
更新
如果你得到文本或符号,你可能必须使用 base64_encode()
。
试试这个:
$img = base64_encode(CallAPI($imgLinkFromValue ));
echo '<img src="data:image/jpeg;base64,' . $img . '" class="img-responsive" />'
您可以随时将 src 中的数据更改为 png 或 gif,具体取决于您的需要。
有关 base64_encode 函数的更多信息,您可以使用此 link。 http://php.net/manual/en/function.base64-encode.php