如何使用php从api中获取特定数据

时间:2018-04-02 11:49:25

标签: php api curl

我是使用API​​获取数据的新手,需要从许多可用列中获取某些特定的列数据。我能够使用API​​获取所有数据,但想知道如何获取特定列。以下是我的代码:

<?php
$url = 'myurl';

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => "{\"username\": \"test\", \"password\": \"test\"}",
    CURLOPT_HTTPHEADER => array(
        "accept: application/json",
        "cache-control: no-cache",
        "content-type: application/json"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}
$request = curl_init();
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($request, CURLOPT_HEADER, FALSE);
$result = curl_exec($request);
curl_close($request);

if ($result) {
    $data = new SimpleXMLElement($result);
    foreach ($data->Item as $entry) {
    echo "XML Activity Name: " . $entry->ActivityName . "<br>";
}
} else {
    exit();
}
?>

0 个答案:

没有答案