2

Im trying to get data from api HERE via POST request to my application and it does return error in error.log.

PHP message: PHP Warning: file_get_contents('url_here') failed to open stream: HTTP request failed! HTTP/1.1 406 Not Acceptable

Here is my code:

$url = 'http://inventory.api.eciaauthorized.com/api/Search/Query';
$fields = array(
  'CompanyID' => 'company_id',
    'APIKey' => 'api_key_here',
    'Queries' => array(
      'SearchToken' => 'query_string_here',
    ),
    'InStockOnly' => true,
    'ExactMatch' => true,
    'IsCrawler' => true
);

$postdata = http_build_query($fields);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/json ' . ' Accept: application/json',
        'content' => $postdata
    )
);

$context = stream_context_create($opts);

$result = file_get_contents($url, false, $context);
var_dump($result);

When I var_dump the result it returns:

false

What I am missing here? Please help. Thanks.

From This Question who have answer It throws the same error.

$result = fopen($url, "r",true, $context);

Some suggest to user CURL so I do it this is my code:

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'Content-Type: application/json'
));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,  $postdata);
curl_setopt($ch, CURLOPT_VERBOSE, true);
//execute post
$result['body'] = curl_exec($ch);
$result['headers'] = curl_getinfo($ch);

if(curl_error($ch))
{echo 'error:' . curl_error($ch);}

var_dump($ch);

curl_close($ch);

When I var_dump the $result it returns

true

and there are no data included and it throws error on

"HTTP/1.1 406 Not Acceptable Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Wed, 24 Aug 2016 06:43:54 GMT Content-Length: 0

Ive done my research on that error and see that the headers are incomplete but I follow properly the API documentation. Please Help. Thanks.

These are on the documentation:

The ECIA Inventory API receives search requests via HTTP POST. It supports both JSON and XML requests and responses. Please indicate with your requests which format you would like to use by including the "Content-Type" and "Accept" headers set to either "application/json" or "application/xml".

The format for the body of the requests and responses is defined below. The url for the API is: https://inventory.api.eciaauthorized.com/api/Search/Query

Community
  • 1
  • 1
Goper Leo Zosa
  • 1,185
  • 3
  • 15
  • 33

0 Answers0