edgar.dueñas
Enviado por edgar.dueñas el Mié, 11/05/2016 - 09:11

Consultando un Json con PHP, es un ejemplo de como hacer una consulta post, para obtener una respuesta en json.

<?php
        //Parametros para consulta al WS
        $action = "ConsultaProvincia";
        $token = md5 ( variable_get('token_api') . date("Ymd") );
        $url = variable_get('webservice_link');
 
        $fields = array(
            'action' => urlencode($action),
            'authKey' => $token
        );
 
        //url-ify the data for the POST
        $fields_string = '';
        foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
        $fields_string = rtrim($fields_string, '&');
 
        //open connection
        $ch = curl_init();
        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
        // receive server response ...
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //execute post
        $server_output = curl_exec($ch);
        //close connection
        curl_close($ch);
        //Json decode
        $result = json_decode($server_output);
 
        foreach($result->array_prov AS $record){
            if($record->idprov != ''){
                $option1[$record->idprov] = $record->provincia;
            }
        }
?>