[PHP] cURLwerkt niet

Pagina: 1
Acties:

Vraag


Acties:
  • 0 Henk 'm!

  • Toppe
  • Registratie: Januari 2004
  • Nu online

Toppe

Oké ✅

Topicstarter
Dag,

PHP is voor mij alweer enige tijd geleden en ik zit vast geroest met een bepaald probleem, hopelijk weet hier iemand het antwoord want ik zie het simpelweg niet.

Ik wil een cURL maken, in postman werkt dit prima:

POST cURL
code:
1
2
3
curl --location 'https://eu-openapi.milesight.com/oauth/token?client_id=XXXXXX-956b-431f-924c-315c1ec6c1b4&client_secret=XXXXXXbz4arYQI4JkCjwpo59577vgfS&grant_type=client_credentials' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode '=x-www-form-urlencoded'


En ik krijg netjes een response:
code:
1
2
3
4
5
6
7
8
9
10
{
    "data": {
        "access_token": "XXXXXXXMxZi05MjRjLTMxNWMxZWM2YzFiNCIsInRzIjoxNzE4OTgwODg5MzYzfQ.H5rTA6LKRzVjMfsLIGFpZzRoF2VWxMkEpps6pliEXzWXXeAv1oZ8gno5jv-oQZMta5JVcmKwa9A7dDNaDNioT6F9ciNJTDSFM2iNChbFiX7ia5759yS-Kl8qIri1JzDlcdI9u2jzS1DlIMqAPp3r5DiOdD4HDS4lz_3mBJ2MhjA",
        "refresh_token": "XXXXXXNmM2FlZDYxNDk5NyIsImNsaWVudF9pZCI6ImY0M2Q4ZjFjLTk1NmItNDMxZi05MjRjLTMxNWMxZWM2YzFiNCIsInRzIjoxNzE4OTgwODg5MzYzfQ.goZJUi3jZKIGqSKGw5gXbrO74lfqx5V3sEVDSH7zfMavSdyuWtDt0PgrZPgBOq_6IOuD2ubBAwJbgmaNB0CeOA9Erbsj5J3UXmmXvkW_E8W9gW--84dk-LcDNQQonjPusXv1LyNGAUBsdDlq1W4foErTHsjiV9PhSj7gcOhnnOc",
        "expires_in": 3599,
        "refresh_token_expires_in": 2591999
    },
    "status": "Success",
    "requestId": "fc27091dc54c6debccaaf987c33baf28"
}


Doe ik dat zelfde in PHP:
code:
1
2
3
4
5
6
7
8
9
10
11
<?php
$payload = [
    "=x-www-form-urlencoded"
];
$cURLConnection = curl_init('https://eu-openapi.milesight.com/oauth/token?client_id=XXXXXX-956b-431f-924c-315c1ec6c1b4&client_secret=XXXXXI4JkCjwpo59577vgfS&grant_type=client_credentials');
curl_setopt($cURLConnection, CURLOPT_HEADER, "Content-Type: application/x-www-form-urlencoded");
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, http_build_query($payload));
$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);
print_r($apiResponse);
?>


Ook dan krijg ik netjes een resultaat. Dat werkt dus. Nu het gekke, ga ik een GET toepassen in postman:
code:
1
2
curl --location 'https://eu-openapi.milesight.com/device/openapi/v1.0/devices/1802648377288925186' \
--header 'Authorization: Bearer XXXXXXXNlN2U5MGExLTBiYTItNDM4Zi05NGZhLWY1M2NmMDdmNjAwZiIsImNsaWVudF9pZCI6ImY0M2Q4ZjFjLTk1NmItNDMxZi05MjRjLTMxNWMxZWM2YzFiNCIsInRzIjoxNzE4OTgxMzQzOTE3fQ.QxN0tg7wDhP7CyKzSw8B43fLey8Ti_RMxcKb3z8XcRrHFFo-jbiEpRxx3aVcGrgk2Cj1O222tLW65l3CjHgCjEagsynr0Oot9VXjsQ1wWSITroD5Rztv8pCbtX99HlG2iu4WcBzhYgNDq6-WS2O-768raFgbt3LpPwmT5E4Nujw'


Dan krijg ik exact het resultaat wat ik wil:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
    "data": {
        "sn": "X",
        "imei": "X",
        "autopStatus": "DISABLE",
        "name": "UG65",
        "model": "UG65",
        "hardwareVersion": "V1.3",
        "firmwareVersion": "60.0.0.43",
        "lastUpdateTime": 1718854092210,
        "application": {
            "applicationId": "X",
            "applicationName": "Device check"
        },
        "project": "Tag1",
        "description": "",
        "deviceId": "X",
        "devEUI": "X",
        "connectStatus": "ONLINE"
    },
    "status": "Success",
    "requestId": "X"
}


Maar, doe ik een zelfde request via PHP maken:
code:
1
2
3
4
5
6
7
8
9
10
11
12
<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://eu-openapi.milesight.com/device/openapi/v1.0/devices/1802648377288925186");
curl_setopt($ch, CURLOPT_HEADER, "Authorization: Bearer XXXXXSI6ImNlN2U5MGExLTBiYTItNDM4Zi05NGZhLWY1M2NmMDdmNjAwZiIsImNsaWVudF9pZCI6ImY0M2Q4ZjFjLTk1NmItNDMxZi05MjRjLTMxNWMxZWM2YzFiNCIsInRzIjoxNzE4OTgxMzQzOTE3fQ.QxN0tg7wDhP7CyKzSw8B43fLey8Ti_RMxcKb3z8XcRrHFFo-jbiEpRxx3aVcGrgk2Cj1O222tLW65l3CjHgCjEagsynr0Oot9VXjsQ1wWSITroD5Rztv8pCbtX99HlG2iu4WcBzhYgNDq6-WS2O-768raFgbt3LpPwmT5E4Nujw");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$list = curl_exec($ch);
curl_close($ch);
print_r(json_decode($list));
?>


dan krijg ik:
code:
1
stdClass Object ( [status] => Failed [requestId] => 5e7b3f72fbbb2bd3cb81d1db2c626e25 [errCode] => token_not_found )


Ergens doe ik wat fout maar aangezien ik geen enkele foutmelding krijg weet ik even niet zo goed waar ik moet zoeken.

Iemand die mij een duwtje weer in de juiste richting kan geven?

Donstil: Je moet kopen wat je wilt hebben. Niet wat je nodig hebt!

Beste antwoord (via Toppe op 22-06-2024 17:55)


  • moijamie
  • Registratie: Augustus 2013
  • Laatst online: 25-09 15:27
@Toppe, Je kan in Postman ook gelijk de PHP code krijgen.

Afbeeldingslocatie: https://tweakers.net/i/ZY8xs3jbNoC_x8uF6KXxyPoiLSo=/800x/filters:strip_exif()/f/image/LSrla3egiPuSUEEmlZhdwJ1L.png?f=fotoalbum_largehttps://tweakers.net/i/ZY8xs3jbNoC_x8uF6KXxyPoiLSo=/800x/filters:strip_exif()/f/image/LSrla3egiPuSUEEmlZhdwJ1L.png?f=fotoalbum_large

[ Voor 143% gewijzigd door RobIII op 24-06-2024 23:54 ]

const { signature } = await fetchProfile()

Alle reacties


Acties:
  • +2 Henk 'm!

  • Cartman!
  • Registratie: April 2000
  • Niet online
CURLOPT_HEADER doet iets anders dan dat jij denkt ;)

[ Voor 39% gewijzigd door Cartman! op 21-06-2024 17:15 ]


Acties:
  • 0 Henk 'm!

  • Toppe
  • Registratie: Januari 2004
  • Nu online

Toppe

Oké ✅

Topicstarter
Thanks, dat was het juiste duwtje!

Donstil: Je moet kopen wat je wilt hebben. Niet wat je nodig hebt!


Acties:
  • Beste antwoord
  • +1 Henk 'm!

  • moijamie
  • Registratie: Augustus 2013
  • Laatst online: 25-09 15:27
@Toppe, Je kan in Postman ook gelijk de PHP code krijgen.

Afbeeldingslocatie: https://tweakers.net/i/ZY8xs3jbNoC_x8uF6KXxyPoiLSo=/800x/filters:strip_exif()/f/image/LSrla3egiPuSUEEmlZhdwJ1L.png?f=fotoalbum_largehttps://tweakers.net/i/ZY8xs3jbNoC_x8uF6KXxyPoiLSo=/800x/filters:strip_exif()/f/image/LSrla3egiPuSUEEmlZhdwJ1L.png?f=fotoalbum_large

[ Voor 143% gewijzigd door RobIII op 24-06-2024 23:54 ]

const { signature } = await fetchProfile()


Acties:
  • 0 Henk 'm!

  • Toppe
  • Registratie: Januari 2004
  • Nu online

Toppe

Oké ✅

Topicstarter
moijamie schreef op zaterdag 22 juni 2024 @ 17:36:
@Toppe, Je kan in Postman ook gelijk de PHP code krijgen.

*snip*
Nog makkelijker :D

[ Voor 32% gewijzigd door RobIII op 23-06-2024 19:23 ]

Donstil: Je moet kopen wat je wilt hebben. Niet wat je nodig hebt!