source/AllegroRestApi.php
/**
     * Sends request to Allegro REST API
     * using given arguments
     *
     * Returns API response as JSON object
     * 
     * @param   string  $resource   Resource path
     * @param   string  $method     Request method
     * @param   mixed   $data       Request body
     * @param   array   $headers    Request headers
     * @param   boolean $json       Should we send $data as JSON?
     * @return  object
     * @throws  RuntimeException
     */
Sends request to Allegro REST API
using given arguments

Returns API response as JSON object


Sends request to Allegro REST API using given arguments Returns API response as JSON object
public function sendRequest($resource, $method, $data = array(), array $headers = array(), $json = true)
    {
        $options = array(
            'http' => array(
                'method'  => strtoupper($method),
                'header'  => $this->parseHeaders($requestHeaders = array_replace(array(
                    'User-Agent'      => 'AsocialMedia/AllegroApi/v3.1.0 (+https://asocial.media)',
                    'Authorization'   => 'Bearer ' . $this->getToken(),
                    'Content-Type'    => 'application/vnd.allegro.public.v1+json',
                    'Accept'          => 'application/vnd.allegro.public.v1+json',
                    'Accept-Language' => 'pl-PL'
                ), $headers)),
                'content' => ($json ? json_encode($data) : $data),
                'ignore_errors' => true
            )
        );
        $response = json_decode(file_get_contents(
            (stristr($resource, 'http') !== false 
                ? $resource 
                : $this->getUrl() . '/' . ltrim($resource, '/')
            ), 
            false, 
            stream_context_create($options)
        ));
        if (isset($response->errors) || isset($response->error_description)) {
            throw new RuntimeException(
                'An error has occurred: ' . print_r($response, true),
                $this->getResponseCode($http_response_header)
            );
        }
        if (!is_object($response)) {
            $response = new \stdClass();
        }
        $response->request_headers  = $requestHeaders;
        $response->response_headers = $http_response_header;
        return $response;
    }
public function sendRequest ( $ resource , $ method , $ data = array ( ) , array $ headers = array ( ) , $ json = true ) { $ options = array ( 'http' => array ( 'method' => strtoupper ( $ method ) , 'header' => $ this -> parseHeaders ( $ requestHeaders = array_replace ( array ( 'User-Agent' => 'AsocialMedia/AllegroApi/v3.1.0 (+https://asocial.media)' , 'Authorization' => 'Bearer ' . $ this -> getToken ( ) , 'Content-Type' => 'application/vnd.allegro.public.v1+json' , 'Accept' => 'application/vnd.allegro.public.v1+json' , 'Accept-Language' => 'pl-PL' ) , $ headers ) ) , 'content' => ( $ json ? json_encode ( $ data ) : $ data ) , 'ignore_errors' => true ) ) ; $ response = json_decode ( file_get_contents ( ( stristr ( $ resource , 'http' ) !== false ? $ resource : $ this -> getUrl ( ) . '/' . ltrim ( $ resource , '/' ) ) , false , stream_context_create ( $ options ) ) ) ; if ( isset ( $ response -> errors ) || isset ( $ response -> error_description ) ) { throw new RuntimeException ( 'An error has occurred: ' . print_r ( $ response , true ) , $ this -> getResponseCode ( $ http_response_header ) ) ; } if ( ! is_object ( $ response ) ) { $ response = new \ stdClass ( ) ; } $ response -> request_headers = $ requestHeaders ; $ response -> response_headers = $ http_response_header ; return $ response ; }
Sends request to Allegro REST API
 using given arguments
Sends request to Allegro REST API using given arguments
// Setting request options // Getting result from API // We have found an error in response // Throwing an exception // Checking if our response is a valid object // Creating an instance of stdClass // Saving response and request headers // Returning response