2

First post here, I'm running into an issue I didn't have when working on my local machine /w XAMPP.

I've push some basic PHP code on my VPS with a fresh Apache2 and PHP7 install, and one of the file is actually doing a file_get_contents to do a GET request on Trello's API:

$lists = json_decode(file_get_contents("https://api.trello.com/1/boards/<<HIDDEN>>/lists?key={$id}&token={$token}"), true);

The problem is that I'm receiving this HTTP response when doing so:

[Tue Aug 24 14:27:14.132294 2021] [:error] [pid 17956] [client <<HIDDEN>>] PHP Warning:  file_get_contents(https://api.trello.com/1/boards/<<HIDDEN>>/lists?key=<<HIDDEN>>&amp;token=<<HIDDEN>>): failed to open stream: HTTP request failed! HTTP/1.1 426 Upgrade Required\r\n in <HIDDEN>/trello.php on line 7, referer: <<HIDDEN>>

I've done some research and I saw that I kinda need to upgrade the call to another HTTP protocol but so far, I have no clues about the way to proceed...

Any advice, tip?

Thanks!

  • 2
    google helps: https://github.com/ConvertAPI/convertapi-php/issues/22 - `$context = stream_context_create(array('http'=>array('protocol_version'=>'1.1')));` – hakre Aug 24 '21 at 16:42
  • This question has an older duplicate already here: https://stackoverflow.com/q/64059867/367456 - it includes the workaround to use curl in the question already. – hakre Aug 24 '21 at 16:47
  • @hakre this question(you mentioned in the link) has not an accepteded answer! – nima Aug 25 '21 at 06:26
  • Yes, but it shows to use curl as a workaround already. Maybe its worth to improve it there so that the information is not that scattered. At least its a cross-reference already. – hakre Aug 25 '21 at 06:38

1 Answers1

1

Ok, so I still have no clues about how to fix that using file_get_contents, but I found a workaround that seems to work using curl.

If some people are also having the issue, I've replaced the initial request with that:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.trello.com/1/boards/<<HIDDEN>>/lists?key={$id}&token={$token}");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $lists = json_decode(curl_exec($ch), true);
    curl_close($ch);

Looks like the curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2); is doing the job.

That's not answering the root question (how to do that with file_get_contents) but if it can help...

Thanks!

  • Perhaps locally you have curl as a HTTP stream wrapper (stream wrappers are used by `file_get_contents('https?://...`) but not on the server which then would explain the different behaviour. https://www.php.net/manual/en/wrappers.http.php - https://www.php.net/manual/en/context.curl.php – hakre Aug 24 '21 at 21:13
  • What I like with your answer is that it shows how to do it with CURL_HTTP_VERSION_2. I'm wondering if HTTP version 1.1 would suffice with the api.trello.com service and also if there is an option that makes curl handle `HTTP/1.1 426 Upgrade Required` automatically. Could you take a look if your XAMPP setup has the CURL fopen/stream wrapper? – hakre Aug 25 '21 at 06:40