0

thank you for taking the time to trying and help, i really appreciate it. Let me first explain what I am doing, I am trying to get an image from news site by reading it's meta tags. So when i run it on my localhost there is no error, but when i upload the script on my Free Online Subdomaine, then the error appears and some links not that i try but some. Here is the error:

Warning: get_headers(): Failed to enable crypto in /home/vol15_7/byethost7.com/b7_27905999/htdocs/cron/news_cron_1.php on line 81

Warning: get_headers(http://rss.cnn.com/~r/rss/edition_world/~3/6uMCnNwt5F4/index.html): failed to open stream: operation failed in /home/vol15_7/byethost7.com/b7_27905999/htdocs/cron/news_cron_1.php on line 81

and the line 81 is :

$url_redirect = get_headers($link);

I tried this solution but it did not work:

stream_context_set_default( [
                'ssl' => [
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                ],
            ]);

I need your help please, here is my code is:

            stream_context_set_default( [
                'ssl' => [
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                ],
            ]);

            $url_redirect = get_headers($link);
            //var_dump($url_redirect);
            $location_redirect = $url_redirect[0];

            if (strpos($location_redirect, " 301 ") !== false){
                $redirect_array = explode("Location: ", $url_redirect[1]);
                $location_url = $redirect_array[1];
            } else {
                $location_url = $link;
            }

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $location_url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

            $result = curl_exec($curl);
            curl_close($curl);

            $htmlentities = htmlentities($result);

            if($htmlentities != ""){
                // if htmlentites is not empty we make the rest
                $htmlentities = htmlentities($result);

                libxml_use_internal_errors(true);
                $htmlDom = new DOMDocument();
                $htmlDom->loadHTML($htmlentities);
                //var_dump($htmlDom);
                libxml_clear_errors();
                $htmlstring = $htmlDom->textContent;
                /// code continues from here to grab the image from meta tags ----------------------------
            }
  • Have you researched the error message yet? https://stackoverflow.com/questions/14078182/openssl-file-get-contents-failed-to-enable-crypto/14078318 – CBroe Feb 18 '21 at 14:24
  • This code is a bit strange - you're using `get_headers` to determine if the link has a redirect, but then you're passing the result to Curl with FOLLOW_LOCATION enabled anyway. You should just be able to pass `$link` straight to the Curl code, and it'll have the added bonus that it won't need the line that's throwing the error at all. – iainn Feb 18 '21 at 14:29
  • @iainn the point is that some sites has a 301 error, so i tried to find a solution to get the redirect link the post as a string so far this was my solution, if you have any better one i would be really grateful :) – Firas Helou Feb 18 '21 at 14:33
  • @CBroe i searched for many other posts but none worked, i tried the one in the link you shared, so now none of the links works and i still get the error. I added this curl_setopt($curl, CURLOPT_SSLVERSION,3); – Firas Helou Feb 18 '21 at 14:36
  • 1
    _“the point is that some sites has a 301 error”_ - a 301 is not an _error_. Please properly explain what actual problem you are trying to solve here with this, because that is still rather unclear. I don’t see how _not_ just letting cURL follow redirects automatically, would have anything to do with your stated intention, to get an image URL out of the meta tags. If you are _not_ following any HTTP redirects, then what meta tags would you want to read in that case anyway? – CBroe Feb 18 '21 at 14:40
  • @CBroe thanks for trying to help, so I added to the code hope it will be better to see things, the reason i did it this way, because not any website i tried worked with get_content() and those classic functions for rss so I had to work my way around it and came up with this way of reading the html as a string, I am not letting curl redirect because it will open the site automatically which i don't want, but instead i want to access its html elements silently – Firas Helou Feb 18 '21 at 14:50
  • 1
    _“I am not letting curl redirect because it will open the site automatically which i don't want, but instead i want to access its html elements silently”_ - no clue what you are trying to say with that, that just does not make any sense. cURL makes HTTP requests, that’s it. Not sure what you are referring to here by “open the site”. – CBroe Feb 18 '21 at 14:52
  • @FirasHelou My "better" solution is just what I originally posted - pass your `$link` variable straight to your Curl code. If there are any redirects, it will follow them automatically, which is what the lines above are already trying to emulate. – iainn Feb 18 '21 at 14:52
  • @iainn it worked on my locahost, it gave me all the output i want but when i uploaded the script online, it did not work and it gave me no error nevertheless i have error output for php on. this ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); – Firas Helou Feb 18 '21 at 15:07

1 Answers1

0

So the solution was suggestion by @iainn and seems working so far, so thanks to @iainn for it.

here is what i did:

            $curl = curl_init();
            // I placed the original $link
            curl_setopt($curl, CURLOPT_URL, $link);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

            $result = curl_exec($curl);
            curl_close($curl);

            $htmlentities = htmlentities($result);