0

I'm attempting a GET request using SSL and basic auth using the file_get_contents function:

$username = "XXXXXXXXXX";
$password = "XXXXXXXXXX";

$url = "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api";

$context = stream_context_create(array("http" => array("header" => "Authorization: Basic " . base64_encode("$username:$password"))));

$data = file_get_contents($url, false, $context);

echo $data;

Here's the error message I get:

Warning: file_get_contents(https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api): failed to open stream: HTTP request failed! HTTP/1.0 500 Server Error...

I've already confirmed that openssl is enabled:

enter image description here

And we might as well get this out of the way up-front:

Why don't you just use cURL?

I could. But I also want to figure out why file_get_contents isn't working. I like the relative simplicity of file_get_contents. Call me crazy.

David Jones
  • 10,117
  • 28
  • 91
  • 139
  • With an other HTTPS URL without basic auth, does it work? – Anthony May 31 '18 at 21:11
  • i can hit the url with my browser, i cant with `file_get_contents`, i suspect they are detecting the automated attempt. with curl you can make it look a lot more like a 'real' person access, you dont have those options with `file_get_contents` –  May 31 '18 at 21:11
  • @AnthonyB yes, it works with `https://google.com` for example. – David Jones May 31 '18 at 21:18
  • @smith interesting...so it may be unique to this endpoint? I'll give a basic cURL setup a try and see if I run into the same issue. – David Jones May 31 '18 at 21:19
  • that's my guess, i only did some basic testing for you. –  May 31 '18 at 21:20
  • In the context, try to set the user agent. If you set the same user agent as a Firefox for example the website will no longer detect an automated request. – Anthony Jun 01 '18 at 06:51

2 Answers2

6

Curiosity is a good thing so it's cool to dig this problem without falling back to cURL before fixing this problem.

<?php
$username = "XXXXXXXXXX";
$password = "XXXXXXXXXX";

$url = "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api";

$context = stream_context_create(array(
    "http" => array(
        "header" => "Authorization: Basic " . base64_encode("$username:$password"),
        "protocol_version" => 1.1, //IMPORTANT IS HERE
    )));

$data = file_get_contents($url, false, $context);

echo $data;

The fact is the server does not support HTTP/1.0. So you haven't any problem with SSL/TLS nor with your user agent. It is just the server that support HTTP from 1.1.

As said in the stream_context_create documentation the default protocol_version used in stream_context_create is 1.0. That's why you got an error 500.

Anthony
  • 2,014
  • 2
  • 19
  • 29
-1

EDIT : My bad, don't see this is not curl. Try with this

$username = "XXXXXXXXXX";
$password = "XXXXXXXXXX";

$url = "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api";

$context = stream_context_create(array(
"http" => array("header" => "Authorization: Basic " . base64_encode("$username:$password")), 
"ssl"=>array(
    "verify_peer"=>false,
    "verify_peer_name"=>false,
)));

$data = file_get_contents($url, false, $context);

echo $data;
Flyzzx
  • 458
  • 5
  • 13