我使用curl作为json对象从api获取数据它给出了一个我希望将其绑定到下拉列表的数组

时间:2016-08-22 11:48:50

标签: php json curl

Array Result please check the link

现在我想把它绑定到下拉怎么做?我不知道使用api数据作为数组创建drio。

<?php
// specify the REST web service to interact with
$baseurl = 'http://host/api/v1/specializations';
/**
  * Authenicate and get back token
  */
$curl = curl_init($baseurl);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"Authorization: Bearer $atoken")); 

// Set the POST arguments to pass to the Sugar server
$rawPOSTdata = array(
    "type" => "private",
    );
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($rawPOSTdata));

// Make the REST call, returning the result
$response = curl_exec($curl);
if (!$response) {
    die("Connection Failure.\n");
}
// Convert the result from JSON format to a PHP array
//echo "Success! response result is $response\n";
//print_r($response);
//var_dump(json_decode($response));


        curl_close($curl);
        if ( isset($result->error) )
        {
            die($result->error_message."\n");
        }
var_dump(json_decode($response, true));
?>

2 个答案:

答案 0 :(得分:0)

请查看是否有帮助。

$arr = json_decode($response);
foreach($arr['items'] as $val)
{
    echo $val['fieldname'].'<br>';       
}

答案 1 :(得分:0)

我认为这就是你的意思 - 无论如何都是这样。我不是100%确定我使用了正确的密钥

<?php

    $baseurl = 'http://host/api/v1/specializations';
    $headers=array(
        "Content-Type: application/json",
        "Authorization: Bearer $atoken"
    );
    $data = array( "type" => "private" );


    $curl = curl_init( $baseurl );
    curl_setopt( $curl, CURLOPT_POST, true );
    curl_setopt( $curl, CURLOPT_HEADER, false );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers ); 
    curl_setopt( $curl, CURLOPT_POSTFIELDS, json_encode( $data ) );
    $result = curl_exec( $curl );
    curl_close( $curl );

    if( $result ) {
        if ( isset($result->error) )die( $result->error_message );
        /* Convert json data to array */
        $arr=json_decode( $result, true );

        /* Items of interest for menu are here? */
        $json=$arr['data']['specialityMap'];

        /* empty array to populate with html content */
        $html=array();
        /* add to the array */
        $html[]="<select name=''>";
        foreach($json as $i => $v )$html[]="<option value='$i'>$v";
        $html[]="</select>";

        /* echo or return html menu */
        echo implode( PHP_EOL, $html );
    }

?>