使用 WordPress,在同一页面上两次调用函数第二次失败 [英] Using WordPress, calling a function twice on same page fails second time

查看:29
本文介绍了使用 WordPress,在同一页面上两次调用函数第二次失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主题的 function.php 文件中有一个函数可以调用 Edmunds API 并检索库存车辆图像.

I have a function in my theme's function.php file that calls to the Edmunds API and retreives a stock vehicle image.

从页面模板中,如果我多次调用该函数,则在第二次调用时会失败.它第一次完美运行,但第二次不会输出任何内容.当我尝试打印 $aVehImage 数组时,它是空的.(顺便说一下,我已经验证了 AP​​I 中的图像可用于辅助调用中的车辆)

From a page template, if I call the function more than once, it fails on the second call. It works perfectly the first time, but doesn't output anything the second time. When I try and print_r the $aVehImage array, it's empty. (I've verified that images are available in the API for the vehicles in the secondary calls, btw)

代码如下:

function get_edmunds_image($vehicleMake, $vehicleModel, $vehicleYear) {

    $getVehicleStyle = 'https://api.edmunds.com/api/vehicle/v2/'.$vehicleMake.'/'.$vehicleModel.'/'.$vehicleYear.'/styles?state=used&fmt=json&api_key=XXX';
    $vehicleStyleID = json_decode(file_get_contents($getVehicleStyle), true);

    $getImages = 'https://api.edmunds.com/v1/api/vehiclephoto/service/findphotosbystyleid?styleId='.$vehicleStyleID['styles'][0]['id'].'&fmt=json&api_key=XXX';
    $aImages = json_decode(file_get_contents($getImages), true);

    $aVehImage = array();

    foreach ($aImages as $image) {
        $iURL = 'http://media.ed.edmunds-media.com'.str_replace('dam/photo','',$image['id']).'_';

        array_push($aVehImage, $iURL);
    }

echo '<img src="'.$aVehImage[0].'500.jpg" />';

}

推荐答案

感谢 Marcos!这确实似乎是问题所在.目前,我只是使用 sleep() 函数暂停一秒钟,直到找到更好的解决方案.

Thanks Marcos! That did, indeed, appear to be the issue. For now, I just used the sleep() function to pause it for a second, until I find a better solution.

这篇关于使用 WordPress,在同一页面上两次调用函数第二次失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆