0

I am working with a custom Silex (Symphony) CMS.

One of the custom modules uses google/apiclient (https://packagist.org/packages/google/apiclient) module for fetching YouTube video channels and video informations.

On our local environment, for some reason, the ampersand (&) of a YouTube request URL gets encoded to &, and for this reason it is not working, throwing failed to open stream: Connection refused error, while if I open a new browser tab, and paste the URL, and change the & into &, it works just fine. Tried to use a USA vpn, but no change.

It could be some PHP setting that I am not aware of. Tried to override manually the fetching URL, but the encode happens insde fopen I guess.

So I tried to use curl instead of fopen to read the URL, inside google/apiclient's Stream.php file, and it reads the URL just fine, it is not throwing any errors. Before that, also tried file_get_contents(), same result as with fopen.

Is there a way to transform a curl result to an fopen stream, to replace only that part of the code, in order to make it work?

Or someone does have another solution for this?

Note: composer update google/apiclient says "Nothing to update", so either the version was locked down, or it is at the latest version.

Andrew Brown
  • 415
  • 1
  • 5
  • 15
  • the `&` thing have nothing to do with php settings, your url is simply not properly html_entity_decode()'ed somewhere. and don't convert `&` back to `&`, just run it through html_entity_decode() instead. because guess what, it's not just `&`, you also have problems with `<` which has become `<`, and `>` which has become `>`, and lots of other edge cases like it. – hanshenrik Sep 11 '18 at 05:54
  • @hanshenrik, thank you for the feedback, that's definitely true and something to keep in mind. – Andrew Brown Sep 13 '18 at 09:12

1 Answers1

0

I figured it out, so I'll leave here for anybody else.

Like I mentioned, in the file google/apiclient/src/Google/IO/Stream.php, around line 129, commented out the set_error_handler part together with the fopen. First I needed to read the content of the URL with curl, using code found here

$fh = fopen('php://temp', 'w+');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_exec($curl);
curl_close($curl);
rewind($fh);

then needed to get the header from the same, which I found how to do here

$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
$response = curl_exec($curl);

$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$http_response_header = substr($response, 0, $header_size);

curl_close($curl);

and those did the trick. Hope it is useful to others as well.

Andrew Brown
  • 415
  • 1
  • 5
  • 15