douvcpx6526
douvcpx6526
2018-10-11 10:32

使用来自API的php和curl命令解析JSON

已采纳

I'm trying to parse the JSON information I get, using PHP, from a powershell script that uses a form on the web-page to send an API request that searches a test database and returns a set of clients and I want to eventually put these into a dynamically populated table.

The issue i'm having is that i'm not able to just print out a specified part of the JSON array onto the page

The JSON i'm getting looks like this:

[{
    "id":          2,
    "firstname":   "Edward",
    "lastname":    "Franks",
    "companyname": "",
    "email":       "EdwardFranks@mail.com",
    "datecreated": "2018-10-09",
    "groupid":     0,
    "status":      "Inactive"
},
{
    "id":          1,
    "firstname":   "Frank",
    "lastname":    "Ti",
    "companyname": "U Consultation",
    "email":       "frank@u.co.uk",
    "datecreated": "2018-10-08",
    "groupid":     0,
    "status":      "Active"
}]

We get this information by searching a name or an email address associated with the account.

HTML:

<form method ="post" action = "veeam.PHP">
    <ul class="form-style-1">
        <li>
            <label>Name or Email <span class="required">*</span></label>
            <input type="text" name="clientsearch" class="field-divided" placeholder="Enter details" />
        <li>
            <input type="submit" name="submit" onSubmit="invokeapi()"/>
        </li>
    </ul>
</form>

PHP:

function invokeapi() {
    $client = $_POST["clientsearch"];

    $username = 'username';
    $password = 'password';
    $url = 'http://apiURL/?command=Check-ClientWHMCS%20'.$client;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));

    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }

    $output = curl_exec($ch);
    curl_close($ch);

    $clientlist = (array) json_decode($output, true);
    print_r($clientlist);
    echo $clientlist[0]["id"];
}

if(isset($_POST['clientsearch'])) {
    invokeapi();
}

When I use the json_last_error command it says there's no issue with the JSON and when I use the isArray command in PHP it says that the variable IS and array so i'm quite stuck on where to go next.

I get some output on the webpage but it just prints out the entirety of the JSON result on a single line with Array ([0] => 1) at the end of it.

Any help on this issue would be massively appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • donpb2823 donpb2823 3年前

    You need to set CURLOPT_RETURNTRANSFER, now curl_exec() will print out the result immediatly instead of returning it as a string in other words $output will not be filled.

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    

    Also your cast to an array with json_decode with true is not necessary.

    点赞 评论 复制链接分享