2021年4月9日星期五

PHP file_get_contents always add & instead of &

I try to create base 64 images with file_get_contents(). But this function alway add & instead of &, and I don't know why.

I've tried to apply urlencode(), str_replace('&', '&', $url), htmlspecialchars_decode() in my image url but it's not work.

I use PHP 7.4.15 on OSX with MAMP

The image is here :

https://dbwstoragevideo.blob.core.windows.net/installation-report-integration/VSB%20%C3%A9nergies%20nouvelles/Montfrech/image_site.png?sv=2019-10-10&st=2020-11-03T13%3A45%3A01Z&se=2030-11-04T13%3A45%3A00Z&sr=c&sp=rl&sig=DORyzL9aTIZ0e%2BCuU5Jf7knOKI692aqWH3XUubr%2FXhM%3D  

My code

private function getReportSectionSiteMap(Parc $parc, DateTime $min_date, DateTime $max_date, array $targets)      {            $image_map_path = $parc->getCheminPhotoMap();          $image_map_extn = pathinfo($image_map_path, PATHINFO_EXTENSION);          $image_map_content = ('' !== $image_map_path && false == is_null($image_map_path)) ? $this->file_get_contents_utf8($image_map_path . '?' . $this->getParameter('url_photo_key')) : null;            $image_ign_path = $parc->getCheminCarteIgn();          $image_ign_extn = pathinfo($image_ign_path, PATHINFO_EXTENSION);          $image_ign_content = ('' !== $image_ign_path && false == is_null($image_ign_path)) ? $this->file_get_contents_utf8($image_ign_path . '?' . $this->getParameter('url_photo_key')) : null;            return [              'img' => $image_map_content,              'section' => 'activity-report-site-map',              'image_map' => (false === is_null($image_map_content)) ? 'data:image/' . $image_map_extn . ';base64,' . base64_encode($image_map_content) : '',              'image_ign' => (false === is_null($image_ign_content)) ? 'data:image/' . $image_ign_extn . ';base64,' . base64_encode($image_ign_content) : '',          ];      }     private function file_get_contents_utf8($url)      {            return file_get_contents ($url);      }  

the response:

    "type": "https://tools.ietf.org/html/rfc2616#section-10",      "title": "An error occurred",      "status": 500,      "detail": "Warning: file_get_contents(https://dbwstoragevideo.blob.core.windows.net/installation-report-integration/VSB énergies nouvelles/Montfrech/image_site.png?sv=2019-10-10&st=2020-11-03T13%3A45%3A01Z&se=2030-11-04T13%3A45%3A00Z&sr=c&sp=rl&sig=DORyzL9aTIZ0e%2BCuU5Jf7knOKI692aqWH3XUubr%2FXhM%3D): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request\r\n",      "class": "ErrorException",    

For example this is not work, I've got the same error :

private function file_get_contents_utf8($url)      {          $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');          $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");                    return file_get_contents(str_replace($entities, $replacements, urlencode($url)));      }  
https://stackoverflow.com/questions/67030132/php-file-get-contents-always-add-amp-instead-of April 10, 2021 at 09:56AM

没有评论:

发表评论