1

how can send $data (some one values->array) to Rest Apigility for make a query and return the result this is my code for send $data

$body = $this->getRequest()->getContent();
    $json = json_decode($body, true);
    $conditions =array(
            'artist'    =>  $json['data']
    );
    $client = new Client();

    $uri = "http://localhost:81/ApigilityNew/public/albums";

    $user        = 'admin';
    $password    = 'admin';

    //url del servicio REST
    $client->setUri($uri);
    //establecer metodo GET-POST
    $client->setMethod('GET');
    //tiempo de peticion
    $client->setOptions(
            [
                    'maxredirects'  => 0,
                    'timeout'       => 60
            ]
    );
    $client->setHeaders(['Accept' => 'application/json', 'Contet-Type' => 'application/json']);
    $client->setAuth($user, $password , $client::AUTH_BASIC);
    $client->setParameterGet($conditions);
    $response = $client->send($client->getRequest());

for get $data on Apigility this:

AlbumResource.php

   /**
     * Fetch a resource
     *
     * @param  array $condiciones
     * @return ApiProblem|mixed
     */
    public function fetch($conditions) //$id
    {
        //return new ApiProblem(405, 'The GET method has not been defined for individual resources');
        return $this->mapper->fetchOne($condiciones);
    }

AlbumMapper.php

  public function fetchOne($conditions) 
    {

        $sql = ' SELECT album.id, album.artist, album.title, pais.nombre_pais, biografia.genero, biografia.ocupacion, biografia.sitio_web
                 FROM album
                    LEFT JOIN pais ON  pais.id_album = album.id
                    LEFT JOIN biografia ON biografia.id_album = album.id
                WHERE 1 = 1 ';

        if (!empty($conditions['criterio_busqueda']))
        {
            $sql = $sql. " AND album.artist LIKE '%".$condiciones['criterio_busqueda']."%'";
        }

        $resultset = $this->adapter->query($sql, array($data));
        $data = $resultset->toArray();
        if (!$data) {
            return false;
        }
        return $data;
    }//end function

How can get a result? Now it only returns unsupport media type from Apigility

Wilt
  • 41,477
  • 12
  • 152
  • 203
BlackHack123
  • 349
  • 1
  • 10

1 Answers1

1

You have a typo in your ContentType header:

'Contet-Type' => 'application/json'

should be:

'Content-Type' => 'application/json'

That is probably why you get a unsupported media type message

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • i'm using this for guide but don't work [link]https://github.com/akrabat/apigility-music-api/blob/master/module/Music/src/Music/V1/Rest/Album/AlbumResource.php – BlackHack123 Jan 19 '16 at 16:06
  • @DiegoA. That application should work fine. Can you be more specific in your about what the problem is. Do you get an error message? What message? When and where do you get it? Please add a quote of the exact message... – Wilt Jan 20 '16 at 08:09