rRosu
1
Buna ziua!
Am nevoie de un pic de ajutor cu acest API
Vreau sa obtin informatiile despre un anumit telefon, asa ca am introdus acest cod intr-un fisier PHP
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://script.google.com/macros/s/AKfycbxNu27V2Y2LuKUIQMK8lX1y0joB6YmG6hUwB1fNeVbgzEh22TcDGrOak03Fk3uBHmz-/exec',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"route": "device-detail",
"key": "nokia_8210_4g-11666"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Primesc aceasta eroare:
411. That’s an error.
POST requests require a Content-length header. That’s all we know.
In Postman functioneaza, dar in PHP nu
Ma poate ajuta cineva sa rezolv aceasta problema?
primul link de pe google.
1 Like
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
3 Likes
rRosu
4
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://script.google.com/macros/s/AKfycbxNu27V2Y2LuKUIQMK8lX1y0joB6YmG6hUwB1fNeVbgzEh22TcDGrOak03Fk3uBHmz-/exec',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"route": "device-detail",
"key": "nokia_8210_4g-11666"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Undefined variable: data_string
Ce trebuie sa contina data_string ?
Nu trebuie sa dai copy paste de acolo. Este doar un exemplu
In cazul tau cred ca este ceea ce vrei sa trimiti la api
1 Like
Just use Guzzle, e mai simplu
1 Like
claudiucjc
(Claudiu Cojocaru)
7
1 Like
iamntz
(Ionuț Staicu)
A împărțit acest subiect
8
3 posts were merged into an existing topic: Just use X, e mai simplu
daca ai nevoie doar sa faci un request cu atat mai mult nu are sens sa aduci guzzle in proiect.
@rRosu muta
{
"route": "device-detail",
"key": "nokia_8210_4g-11666"
}
intr-o variabila separata. Poti sa-i zici $data_string.
Ideea e ca in header-ul Content-Length trebuie sa trimiti lungimea stringului json pe care il trimiti in CURLOPT_POSTFIELDS.
Eu as face asa
- encodez array-ul meu cu date ca json
$data_string = json_encode([ 'route' => 'device-detail', 'key' => 'nokia_8210_4g-11666']);
iar apoi il trimit catre
CURLOPT_POSTFIELDS => $data_string
si lungimea stringului o trimit in headerul Content-Length
'Content-Length: '. strlen($data_string)
Rezultatul final fiind
<?php
$curl = curl_init();
$data_string = json_encode([
'route' => 'device-detail',
'key' => 'nokia_8210_4g-11666'
]);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://script.google.com/macros/s/AKfycbxNu27V2Y2LuKUIQMK8lX1y0joB6YmG6hUwB1fNeVbgzEh22TcDGrOak03Fk3uBHmz-/exec',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
),
));
$response = curl_exec($curl);
1 Like