从json对象获取图像URL不起作用

时间:2017-08-02 09:17:45

标签: php json object

我正在尝试解码json对象,以便从中获取图像URL。通常这样可以正常工作,但不知何故,以下代码对我不起作用。

$partimages1 = $prods['images'];
$partsimg1 = json_decode($partsimages1);

if($partsimg1->image_intro != ''){
  $productimages = '<img class="kw-prodimage-img" src="cms/'.$partsimg1->image_intro.'" alt="Product 1" title="Product 1" />';
}else{
  $productimages = '<img class="kw-prodimage-img" src="images/backgroundheader.jpg" alt="Product 1" title="Product 1" />';
}

上面的代码总是返回backgroundheader.jpg,而对象内部有一个图像。

当我回显$ partimages1时,这就是结果:

{"image_intro":"images\/Afbeeldingen\/lassen2.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}

出了什么问题?

1 个答案:

答案 0 :(得分:4)

您的拼写错误$partimages1改为$partsimg1 = json_decode($partimage1);

RestTemplate restTemplate = new RestTemplate();
HttpHeaders header = new HttpHeaders();
String auth = username + ":" + password;
byte [] authentication = auth.getBytes();
byte[] base64Authentication = Base64Utils.encode(authentication);
String baseCredential = new String(base64Authentication);
header.add(HttpHeaders.AUTHORIZATION, "Basic " + baseCredential);
             header.setAccept(Collections.singletonList(MediaType.TEXT_PLAIN));

HttpEntity<String> request = new HttpEntity<String>(header);
restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, request, String.class);