S
S
serNevajno2017-09-14 14:11:02
PHP
serNevajno, 2017-09-14 14:11:02

How to send a request using the GET method using the REST protocol?

Hello uv. community. Help me to understand. There is a supplier who gives data about the product using the REST protocol. In the description, the supplier specifies:
Request URL,
HTTP method: GET,
Request headers:
Authorization: Bearer [token]
Accept: application/json
Content-type: application/json
Request parameters:
kod_proizvoditelya (an array containing a list of manufacturers' product codes)
Tried two options:
1st

$opts = array(
    'http'=>array(
        'method'=> "GET",
        'header' => "Content-Type: application/json\r\n".
                    "Accept: application/json\r\n".
                    "Authorization: Bearer туттокен\r\n",
        'content' => array("kod_proizvoditelya=45346457"),
    )
);

$context = stream_context_create($opts);

$file = file_get_contents('https://урлсервера', false, $context);
$data = json_decode($file);
print_r($data);

2nd
class Dadata
{
    public function ApiPos($fields)
    {
        $result = false;
        if ($ch = curl_init("https://урлсервера"))
        {
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Accept: application/json',
                'Authorization: Bearer туттокен'
            ));
            curl_setopt($ch, CURLOPT_HTTPGET, 1);
            // json_encode
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
            $result = curl_exec($ch);
            $result = json_decode($result, true);
            curl_close($ch);
        }
        return $result;
    }
}
$result = Dadata::ApiPos(array("kod_proizvoditelya" =>array( "5745745")));
print_r($result);

In this case, it returns an Array ( [name] => Forbidden [message] => Access denied [code] => 0 [status] => 403 )
Tell me what I'm doing wrong?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Kozhin, 2017-09-14
@maximkozhin

public function ApiPos($fields)
{
  $ch = curl_init($serverUrl. '?' .http_build_query($fields));
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json',
    "Authorization: Bearer {$token}"
  );
  $result = curl_exec($ch);
  $result = json_decode($result, true);
  curl_close($ch);
  return $result;
}

N
n-fom, 2017-09-14
@n-fom

Try first to perform the operation through the rest console
For example: https://chrome.google.com/webstore/detail/rest-con...

A
Alexander Filippenko, 2017-09-14
@alexfilus

Try using Postman to ensure that the correct response is returned to the request, and then transfer it to the code and send it via curl.

R
Rastishka, 2017-09-15
@Rastishka

function dadata($url, $data) {
    $options = [
        'http' => [
            'method'  => 'POST',
            'header'  => [
                'Content-type: application/json',
                'Authorization: Token ' . DADATA_TOKEN,
                'X-Secret: ' . DADATA_SECRET,
            ],
            'content' => json_encode($data),
        ],
    ];
    $context = stream_context_create($options);
    $contents = file_get_contents($url, false, $context);
    return json_decode($contents)[0];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question