Execute a market API request

@param $segment
@param array $parameters
@return array
338469
public function marketRequest($segment, array $parameters=[]) {
        $baseUrl = $this->market_url;
        return $this->privateRequest($baseUrl, $segment, $parameters);
    }
437805
public function indexAction(Request $request, $segmentId = null)
    {
        /** @var WidgetManager $widgetManager */
        $widgetManager = $this->get('kunstmaan_dashboard.manager.widgets');
        /** @var DashboardWidget[] $widgets */
        $widgets = $widgetManager->getWidgets();
        $segmentId = $request->query->get('segment');

        return $this->render('KunstmaanDashboardBundle:Dashboard:index.html.twig', array('widgets' => $widgets, 'id' => $segmentId));
    }
245982
public function PostSegmentsRefresh(XsInt $segmentId)
    {
        $url = sprintf("segments/refresh/%s", $segmentId);
        return new ApiSegmentRefresh($this->execute($url, 'POST'));
    }
1354421
protected function processRequest(array $parameters = [])
    {
        if ($this->nextToken) {
            $parameters['starting_after'] = $this->nextToken;
        }

        if (isset($parameters[0])) {
            $id = $parameters[0];

            unset($parameters[0]);

            if (isset($parameters[1])) {
                $parameters = $parameters[1];

                unset($parameters[1]);
            }

            $parameters = [ $id, $parameters ];
        } else {
            $parameters = [ $parameters ];
        }

        $result = call_user_func_array([ $this->api, 'all' ], $parameters);

        $this->nextToken = $result['has_more'] ? end($result['data'])['id'] : false;

        return $result['data'];
    }
1269260
private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, array &$errors = null): array
    {
        $id = $segment[0];

        $jsonsegment = [];
        if (isset($xmlMap[$id])) {
            $attributes = $xmlMap[$id]['attributes'];
            $details_desc = $xmlMap[$id]['details'];

            $jsonelements = ["segmentIdx" => $segmentIdx, "segmentCode" => $id];
            foreach ($segment as $idx => $detail) {
                $n = $idx - 1;
                if ($idx == 0) {
                    continue;
                }

                if (!isset($details_desc[$n])) {
                    $errors[] = [
                        "text"      => $this->messageTextConf['TOOMANYELEMENTS'],
                        "position"  => $segmentIdx,
                        "segmentId" => $id,
                    ];
                    $jsonelements["Extension" . $n] = $detail;
                    continue;
                }

                $d_desc_attr = $details_desc[$n]['attributes'];

                $jsoncomposite = [];
                if (
                    $detail !== ''
                    &&
                    isset($details_desc[$n]['details'])
                ) {
                    $sub_details_desc = $details_desc[$n]['details'];
                    if (\is_array($detail)) {
                        foreach ($detail as $d_n => $d_detail) {
                            if (!isset($sub_details_desc[$d_n])) {
                                $errors[] = [
                                    "text"      => $this->messageTextConf['TOOMANYELEMENTS_COMPOSITE'],
                                    "position"  => $segmentIdx . "/" . $idx,
                                    "segmentId" => $id,
                                ];
                                $jsoncomposite["CompositeExtension" . $d_n] = $d_detail;

                                continue;
                            }

                            $d_sub_desc_attr = $sub_details_desc[$d_n]['attributes'];
                            if (!isset($jsoncomposite[$d_sub_desc_attr['name']])) { //New
                                $jsoncomposite[$d_sub_desc_attr['name']] = $d_detail;
                            } elseif (\is_string($jsoncomposite[$d_sub_desc_attr['name']])) { // More data than one string
                                $jsoncomposite[$d_sub_desc_attr['name']] = [
                                    $jsoncomposite[$d_sub_desc_attr['name']],
                                    $d_detail,
                                ];
                            } else { // More and more
                                $jsoncomposite[$d_sub_desc_attr['name']][] = $d_detail;
                            }
                        }
                    } else {
                        $d_sub_desc_attr = $sub_details_desc[0]['attributes'];
                        $jsoncomposite[$d_sub_desc_attr['name']] = $detail;
                    }
                } else {
                    $jsoncomposite = $detail;
                }

                if (\array_key_exists($d_desc_attr['name'], $jsonelements)) {
                    $jsonelements[$d_desc_attr['name'] . $n] = $jsoncomposite;
                } else {
                    $jsonelements[$d_desc_attr['name']] = $jsoncomposite;
                }

            }
            $jsonsegment['key'] = $attributes['name'];
            $jsonsegment['value'] = $jsonelements;

        } elseif ($xmlMap !== $this->xmlSvc) {
            $jsonsegment = $this->processSegment($segment, $this->xmlSvc, $segmentIdx, $errors);
        } else {
            $jsonsegment['key'] = 'UnrecognisedType';
            $jsonsegment['value'] = $segment;
        }

        return $jsonsegment;
    }
1158591
private function processService(array &$segments): array
    {
        // init
        $processed = [];

        foreach ($segments as &$seg) {
            $jsonsegment = $this->processSegment($seg, $this->xmlSvc, null);
            $processed[$jsonsegment['key']] = $jsonsegment['value'];
        }

        return $processed;
    }
337527
protected function request($url, $parameters = array(), $request = false)
        {
            $this->lastRequest = $url;
            $this->lastRequestData = $parameters;
            $this->responseHeaders = array();

            $curl = curl_init($url);

            $curlOptions = array(
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_REFERER        => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HEADERFUNCTION => array($this, 'parseHeader'),
            );

            if (! empty($parameters) || ! empty($request)) {
                if (! empty($request)) {
                    $curlOptions[ CURLOPT_CUSTOMREQUEST ] = $request;
                    $parameters = http_build_query($parameters);
                } else {
                    $curlOptions[ CURLOPT_POST ] = true;
                }

                $curlOptions[ CURLOPT_POSTFIELDS ] = $parameters;
            }

            curl_setopt_array($curl, $curlOptions);

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

            $this->lastRequestInfo = curl_getinfo($curl);

            curl_close($curl);

            if (! empty($error)) {
                throw new \Exception($error);
            }

            return $this->parseResponse($response);
        }
720353
public function httpPostRequest($url, $parameters)
    {
        if (empty($this->client)) {
            $this->client = new Client(['verify' => false]);
        }

        $this->response = $this->client->request(
            'POST',
            $url,
            [
                'headers' => $this->getHeaders(),
                'json' => $parameters
            ]
        );
        $response = json_decode($this->response->getBody());

        return $response;
    }
1268417
public function api($url, $method, array $params = [], array $headers = [])
    {
        try {
            return $this->sendRequest($method, $url, $params, $headers);
        } catch (\Exception $e) {
            sleep(2);
            return $this->sendRequest($method, $url, $params, $headers);
        }
    }
882470
protected function apiRequest($url, $params = array()) {
        if(! isset($params['api_key']) && $this->hasAttribute('api_key'))
            $params['key'] = $this->getAttribute('api_key');

        $query = http_build_query($params, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url . '?' . $query);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);

        return $output;
    }
433265
protected function execute($path, $method, $queryParams, $httpBody = null, $headers = array())
    {
        $attempts = $this->attempts;
        $response = $this->apiClient->call($path, $method, $queryParams, $httpBody, $headers);

        while ($response->getCode() == 202 && $attempts > 0) {
            $this->delay($response);
            $attempts--;
            $response = $this->apiClient->call($path, $method, $queryParams, $httpBody, $headers);
        }

        return $response;
    }
319940
private function doGetRequest($target, $parameters)
	{
		// Execute the GET request to the given target, along with the given parameters.
		$guzzle = $this->client->getGuzzle();
		$response = $guzzle->get($target, ['query' => $parameters]);
		return $response;
	}
1123289
protected function sendTransaction(array $parameters, $url = null, $post = true, array $headers = ['Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1'])
    {
        if ($url === null) {
            $url = $this->url['transactions'];
        }

        $parameters = $this->array_filter_recursive($parameters);

        $data = '';
        foreach ($parameters as $key => $value) {
            $data .= $key.'='.$value.'&';
        }
        $parameters = rtrim($data, '&');

        $method = 'POST';

        if (!$post) {
            $url .= '?'.$parameters;
            $parameters = null;
            $method = 'GET';
        }

        $result = $this->executeCurl($parameters, $url, $headers, $method);

        return $this->formatResult($result);
    }
723174
public function execute(array $params = [])
    {
        $context = \Context::getInstance();
        $excludedRoutes = $this->getExcludedRoutes();
        $currentRoute = [
            'extension' => $context->getExtensionName(),
            'module' => $context->getModuleName(),
            'action' => $context->getActionName(),
        ];

        if (!in_array($currentRoute, $excludedRoutes)) {
            $currentUrl = \common_http_Request::currentRequest()->getUrl();
            $url = $this->url . (parse_url($this->url, PHP_URL_QUERY) ? '&' : '?') . 'return_url=' . urlencode($currentUrl);

            $flowController = new FlowController();
            $flowController->redirect($url);
        }
    }
486403
protected function request($parameters, $method = 'GET')
    {
        $formParams = $this->getFormParams($parameters);

        if (!isset($formParams['api_key'])) {
            return null;
        }

        if ($method === 'POST') {
            // split params
            $response = $this->httpClient->request($method, Api::PATH_TO_API_SCRIPT, array(
                'query' => $this->getQueryParams($parameters),
                'form_params' => $formParams
            ));
        } else {
            // all params as query GET params
            $response = $this->httpClient->request($method, Api::PATH_TO_API_SCRIPT, array(
                'query' => array_merge($this->getQueryParams($parameters), $formParams),
            ));
        }

        return $response;
    }
454013
public function getData($segments){
		$data=new \stdClass();
		$this->json=new \stdClass();
		$this->getDataJson($segments, $this->json);

		$data->error=$this->error;
		$data->json=$this->json;
		$this->json->error=$this->error;
		return $data;
		}
35898
protected function executeRequest($url, $curl_params = array())
    {
        $defaultParams = array(
            CURLOPT_HEADER => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLINFO_HEADER_OUT => true,
            CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
            CURLOPT_USERPWD => $this->key.':',
            CURLOPT_HTTPHEADER => array( 'Expect:' ),
            CURLOPT_SSL_VERIFYPEER => config('app.env') === 'local' ? 0 : 1,
            CURLOPT_SSL_VERIFYHOST => config('app.env') === 'local' ? 0 : 2 // value 1 is not accepted https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYHOST.html
        );

        $curl_options = array();
        foreach ($defaultParams as $defkey => $defval) {
            if (isset($curl_params[$defkey])) {
                $curl_options[$defkey] = $curl_params[$defkey];
            } else {
                $curl_options[$defkey] = $defaultParams[$defkey];
            }
        }
        foreach ($curl_params as $defkey => $defval) {
            if (!isset($curl_options[$defkey])) {
                $curl_options[$defkey] = $curl_params[$defkey];
            }
        }

        list($response, $info, $error) = $this->executeCurl($url, $curl_options);

        $status_code = $info['http_code'];
        if ($status_code === 0 || $error) {
            throw new PrestashopWebServiceException('CURL Error: '.$error);
        }

        $index = $info['header_size'];
        if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] !== 'HEAD') {
            throw new PrestashopWebServiceException('Bad HTTP response');
        }

        $header = substr($response, 0, $index);
        $body = substr($response, $index);

        $headerArray = array();
        foreach (explode("\n", $header) as $headerItem) {
            $tmp = explode(':', $headerItem, 2);
            if (count($tmp) === 2) {
                $tmp = array_map('trim', $tmp);
                $headerArray[$tmp[0]] = $tmp[1];
            }
        }

        if (array_key_exists('PSWS-Version', $headerArray)) {
            $this->isPrestashopVersionSupported($headerArray['PSWS-Version']);
            $this->version = $headerArray['PSWS-Version'];
        }

        $this->printDebug('HTTP REQUEST HEADER', $info['request_header']);
        $this->printDebug('HTTP RESPONSE HEADER', $header);

        if ($curl_params[CURLOPT_CUSTOMREQUEST] == 'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] == 'POST') {
            $this->printDebug('XML SENT', urldecode($curl_params[CURLOPT_POSTFIELDS]));
        }
        if ($curl_params[CURLOPT_CUSTOMREQUEST] != 'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') {
            $this->printDebug('RETURN HTTP BODY', $body);
        }

        return array(
            'status_code' => $status_code,
            'response' => $body,
            'header' => $header,
            'headers' => $headerArray
            );
    }