Answer the question
In order to leave comments, you need to log in
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);
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);
Answer the question
In order to leave comments, you need to log in
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;
}
Try first to perform the operation through the rest console
For example: https://chrome.google.com/webstore/detail/rest-con...
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.
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 questionAsk a Question
731 491 924 answers to any question