在Wordpress中显示来自api的数据

时间:2019-07-06 20:07:08

标签: php wordpress rest api plugins

我有一个休息用的api,里面有药物和每种药物的信息。

api / v1 / medicine / 返回此

{
    "success": true,
    "data": [
        {
            "medicineId": 12,
            "medicineName": "Abacavir"
        },
        {
            "medicineId": 10,
            "medicineName": "Alclometasone"
        },
        {
            "medicineId": 15,
            "medicineName": " Alectinib"
        },
        {
            "medicineId": 13,
            "medicineName": "Amiloxate"
        }

和 api / v1 / medicine / ID  返回有关药物的信息

{
    "success": true,
    "data": {
        "medicineId": 16,
        "medicineName": " Alendronic acid",
        "medicineDescription": "Alendronic acid is a bisphosphonate that is used for the treatment of some forms of osteoperosis and Paget's disease . It functions by preventing resorption of bone ",
        "sideEffects": "you may experience whilst taking alendronic acid are stomach pain, indigestion or acid reflux,flatulence or bloating, constipation or diarrhoea and muscle, joint or bone pain.",
        "chemicalFormula": "C4H13NO7P2",
        "indication": "Alendronic acid is indicated for the treatment and prevention of osteoporosis in men and postmenopausal women, treatment of glucocorticoid-induced osteoporosis, and Paget's disease of bone. However, alendronic acid is not indicated for use in pediatric populations or patients with a creatinine clearance <35mL/min.",
        "associatedCondition": "Osteogenesis Imperfecta\r\nOsteoporosis\r\nOsteoporosis caused by glucocorticoid\r\nPaget's Disease",
        "alternatives": [],
        "categories": [
            "Agents Causing Muscle Toxicity",
            "Bone Density Conservation Agents",
            "Bisphosphonates"
        ]
    },
    "message": "Successfully retrieved"
}

我想显示药品列表,当我单击药品时,它会显示一个弹出窗口,其中包含有关该药品的信息 WordPress内的所有内容

我尝试过

<?php
$age = file_get_contents('http://link/rest/v1/medicine/');
$array = json_decode($age, true);
$medicine_names = [];
foreach($array['data'] as $key=>$value)
{
 echo ($value['medicineName']). '<br/>' ;    
}
?>

列出哪些药物

有什么想法可以在wordpress中实现吗?

1 个答案:

答案 0 :(得分:0)

也许有帮助

Cannot add or update a child row: a foreign key constraint fails (`backend`.`contacts`, CONSTRAINT `contacts_ibfk_1` FOREIGN KEY (`id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

或使用ajax将数据发送到弹出窗口

另一个例子

function getData(string $route): array
{
    return json_decode(file_get_contents($route), true);
}

$medicineInfo = [];

foreach(getData('http://148.251.195.245:8080/MediHelp/rest/v1/medicine/')['data'] as $medicine) {
    $medicineInfo[$medicine['medicineId']] = getData(
        sprintf('http://148.251.195.245:8080/MediHelp/rest/v1/medicine/%s', $medicine['medicineId'])
    )['data'];
}

//for example
foreach($medicineInfo as $info): ?>
    <a href="#popup-for-<?= $info['medicineId'] ?>"><?= $info['medicineId'] ?></a>
    <!-- popup code -->
    <div id="#popup-for-<?= $info['medicineId'] ?>"><!-- ...$info... --></div>
<?php endforeach ?>