douxiong1994 2019-07-06 20:07
浏览 158
已采纳

在wordpress中显示来自api的数据

I have an a rest api, it has medicines and information about each medicine.

api/v1/medicine/ Returns this

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

and api/v1/medicine/ID returns info about a medicine

{
    "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
Osteoporosis
Osteoporosis caused by glucocorticoid
Paget's Disease",
        "alternatives": [],
        "categories": [
            "Agents Causing Muscle Toxicity",
            "Bone Density Conservation Agents",
            "Bisphosphonates"
        ]
    },
    "message": "Successfully retrieved"
}

I want to show a list a medicines and when i click on a medicine, it shows a popup with the info about that medicine All of that inside wordpress

I tried this

<?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/>' ;    
}
?>

Which lists the medicines

Any idea how i can achieve that in wordpress?

  • 写回答

1条回答 默认 最新

  • dongpu9481 2019-07-06 21:58
    关注

    maybe this help

    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 ?>
    

    or use ajax for send data to popup

    another example

    foreach(getData('http://148.251.195.245:8080/MediHelp/rest/v1/medicine/')['data'] as $medicine) {
        $info = getData(
            sprintf('http://148.251.195.245:8080/MediHelp/rest/v1/medicine/%s', $medicine['medicineId'])
        )['data'];?>
    
        <a href="#popup-for-<?= $info['medicineId'] ?>"><?= $info['medicineId'] ?></a>
        <!-- popup code -->
        <div id="#popup-for-<?= $info['medicineId'] ?>"><!-- ...$info... --></div>
    <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动