如何在php中将webspage作为数组中忽略无效的ssl证书?

时间:2016-07-17 11:02:36

标签: php arrays ssl

我有一台打印机(实际上有几台),我想得到它们的状态页面并让一个小的dom解析器告诉我什么时候耗材很少。没什么大不了的,除了打印机在加载之前抛出无效的ssl证书警告。我可以使用wget来获取这样的页面:

      //MUSIC
            try
            {
                Console.WriteLine("Downloading Music...");

                string SourcePathMUSIC = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);

                string DestinationPathMUSIC = DRIVELOCATION + @"\DOWNLOADEDDATA\music";
                foreach (string dirPath in Directory.GetDirectories(SourcePathMUSIC, "*", System.IO.SearchOption.AllDirectories))
                {
                    Directory.CreateDirectory(dirPath.Replace(SourcePathMUSIC, DestinationPathMUSIC));
                }

                foreach (string newPath in Directory.GetFiles(SourcePathMUSIC, "*.*", System.IO.SearchOption.AllDirectories))
                {
                    File.Copy(newPath, newPath.Replace(SourcePathMUSIC, DestinationPathMUSIC), true);
                    Console.WriteLine(newPath);
                }
                Console.WriteLine("------------------------------------------------------------------");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Music Downloaded");
                Console.ForegroundColor = CONSOLECOLOUR;
                Console.WriteLine("------------------------------------------------------------------");
            }

            catch
            {
                string SourcePathMUSIC = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
                string DestinationPathMUSIC = DRIVELOCATION + @"\DOWNLOADEDDATA\music";
                foreach (string dirPath in Directory.GetDirectories(SourcePathMUSIC, "*", System.IO.SearchOption.TopDirectoryOnly))
                {
                    Directory.CreateDirectory(dirPath.Replace(SourcePathMUSIC, DestinationPathMUSIC));
                }

                foreach (string newPath in Directory.GetFiles(SourcePathMUSIC, "*.*", System.IO.SearchOption.TopDirectoryOnly))
                {
                    File.Copy(newPath, newPath.Replace(SourcePathMUSIC, DestinationPathMUSIC), true);
                    Console.WriteLine(newPath);
                }
                Console.ForegroundColor = ConsoleColor.Black;
                Console.WriteLine("Unable To Copy Some Files, User Has Proctection On Music, You Will Need To Manually Copy The Remaining");
            }
            Console.ForegroundColor = CONSOLECOLOUR;
            Console.WriteLine("------------------------------------------------------------------");
            Console.ForegroundColor = CONSOLECOLOUR;
            Console.WriteLine("                       DOWNLOAD COMPLETE....");
            Console.WriteLine("------------------------------------------------------------------");
            Console.ReadKey();
        }
        catch(Exception ex)
        {
            var CONSOLECOLOR = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("");
            Console.WriteLine(ex.Message);
            Console.ForegroundColor = CONSOLECOLOR;
            //System.Threading.Thread.Sleep(2000);
            //Environment.Exit(1);
            Console.ReadKey();
        }

但我宁愿只使用php,如果它不太复杂,不能这样做。

这是我如何将页面下载为数组的基本示例:

$wget -m --no-check-certificates 10.4.102.125

如何在获取webspage时忽略无效的ssl证书,只是转到我想要获取的页面,作为php中的数组?

我不想更改我的服务器设置。此外,我从未想过如何成功升级这些打印机上的ssl证书。

2 个答案:

答案 0 :(得分:1)

您可以使用curl

要跳过无效的SSL warrning,请使用以下选项:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

例如

<?php 
        // create curl resource 
        $ch = curl_init(); 

        // set url 
        curl_setopt($ch, CURLOPT_URL, "http://10.4.102.125"); 

        //return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        // $output contains the output string 
        $output = curl_exec($ch); 

        // close curl resource to free up system resources 
        curl_close($ch);      
?>

答案 1 :(得分:1)

由于您使用包装器来检索需要设置context options and parameters的页面。在这种情况下,您要查找的选项属于SSL(也适用于HTTPS),我相信您要禁用此选项:

  

verify_peer布尔

     

要求验证所使用的SSL证书。

设置上下文后,需要将其作为第三个参数传递给file

array file ( string $filename [, int $flags = 0 [, resource $context ]] )