0

Having trouble putting together a CURL get request in PHP 8.

The raw curl is as such:

curl --location 'https://api-stg.syf.com/v1/dpos/utility/lookup/transaction/18a1ddb84d4PI2082504896426?lookupType=PARTNERID&lookupId=PI20825048' \
--header 'X-SYF-ChannelId: DY' \
--header 'X-SYF-Request-TrackingId: 123e4567-e89b-12d3-a456-426614174000' \
--header 'Authorization: Bearer iEXgd43NhPA2fa4TiNjiTo0NkKt0' \

My attempt is as such:

<?php
$bearer = "iEXgd43NhPA2fa4TiNjiTo0NkKt0";
$cURLConnection = curl_init();

curl_setopt($cURLConnection, CURLOPT_URL, 'https://api-stg.syf.com/v1/dpos/utility/lookup/transaction/18a1ddb84d4PI2082504896426?lookupType=PARTNERID&lookupId=PI20825048');
$authorization = "Authorization: Bearer ".$bearer;
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($cURLConnection);
curl_close($cURLConnection);
$jsonArrayResponse = json_decode($result);

print_r($jsonArrayResponse);

My call results in "Invalid request.Verify content-Type header and payload of request "

  • 2
    Is that a genuine access token - if so you may need to change it ASAP (or ask a mod to remove it from the question & history) – Nigel Ren Aug 22 '23 at 16:21
  • The error message doesn't say anything about the bearer token. You never provided the JSON body. – Barmar Aug 22 '23 at 16:22
  • @Barmar it's a GET so I don't think there'd be a body. – ADyson Aug 22 '23 at 16:48
  • What's missing is mainly the X-SYF... headers, I think. And the original doesn't have a Content-Type so it's unclear why you added that (especially for a GET request). Did you try an automated tool like https://incarnate.github.io/curl-to-php/ ? It's not always perfect, but it suggests the following code: – ADyson Aug 22 '23 at 16:50
  • `$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api-stg.syf.com/v1/dpos/utility/lookup/transaction/18a1ddb84d4PI2082504896426?lookupType=PARTNERID&lookupId=PI20825048'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); $headers = array(); $headers[] = 'X-Syf-Channelid: DY'; $headers[] = 'X-Syf-Request-Trackingid: 123e4567-e89b-12d3-a456-426614174000'; $headers[] = 'Authorization: Bearer iEXgd43NhPA2fa4TiNjiTo0NkKt0'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);` – ADyson Aug 22 '23 at 16:50
  • `$result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); }curl_close($ch);` . You do need to get an understanding of what each of the bits of the request are though, because your PHP version looks somewhat like the result of guesswork, rather than a step-by-step attempt to replicate the components of the original request. – ADyson Aug 22 '23 at 16:50
  • If there's no body, you shouldn't have `Content-type:`. – Barmar Aug 22 '23 at 16:51
  • [convert CLI curl to PHP](https://stackoverflow.com/questions/1939609/convert-command-line-curl-to-php-curl) – Barmar Aug 22 '23 at 16:51
  • @Barmar well the original doesn't have a content-type header either. the OP seems to have added it arbitrarily, and omitted other headers which _were_ present. – ADyson Aug 22 '23 at 16:51
  • @ADyson And the original presumably has more after the last escaped newline. – Barmar Aug 22 '23 at 16:59
  • @Barmar that is certainly a possibility yes – ADyson Aug 22 '23 at 17:04

1 Answers1

2

I'd start by first removing the Content-Type header. The API could be ignoring the parameters you've set in the URL when sending this header and might even expect JSON instead of URL-based parameters.

If that isn't the issue, you may need to add this:

curl_setopt($cURLConnection, CURLOPT_UNRESTRICTED_AUTH, true);

CURLOPT_UNRESTRICTED_AUTH

true to keep sending the username and password when following locations (using CURLOPT_FOLLOWLOCATION), even when the hostname has changed.

If that doesn't fix it, you should be aware your CLI-based request and PHP code are not equal:

  • You did not include the X-SYF-ChannelId header in the PHP request.
  • You did not include the X-SYF-Request-TrackingId header in the PHP request.
  • (Already addressed) You included Content-Type in your PHP code but not in your CLI request.

If it still isn't working, you can temporarily add:

curl_setopt($cURLConnection, CURLINFO_HEADER_OUT, true);
$result = curl_exec($cURLConnection);
$headersOut = curl_getinfo($cURLConnection, CURLINFO_HEADER_OUT);
print_r($headersOut);

This will print the headers curl actually sent.

Remy
  • 21
  • 2