服务器啊服务器的, 其实很麻烦的. 命令行最方便了.

奇怪的问题

  • 不执行或者奇怪的问题咋办?

  • 重开一个terminal窗口, 基本就ok了.

如何搞?

参考这里: https://segmentfault.com/a/1190000002949021
官方: http://php.net/manual/zh/features.commandline.php

# 查看PHP的所有命令行参数,使用 php -h命令。
php -h
# 下面是详解
-a               以交互式shell模式运行 #这是一个重要的模式
-c <path>|<file> 指定php.ini文件所在的目录
-n               指定不使用php.ini文件
-d foo[=bar]     定义一个INI实体,key为foo,value为'bar'
-e               为调试和分析生成扩展信息
-f <file>        解释和执行文件<file>.
-h               打印帮助
-i               显示PHP的基本信息
-l               进行语法检查 (lint)
-m               显示编译到内核的模块
-r <code>        运行PHP代码<code>,不需要使用标签 <?..?>#
-B <begin_code>  在处理输入之前先执行PHP代码<begin_code>
-R <code>        对输入的没一行作为PHP代码<code>运行
-F <file>        Parse and execute <file> for every input line
-E <end_code>    Run PHP <end_code> after processing all input lines
-H               Hide any passed arguments from external tools.
-S <addr>:<port> 运行内建的web服务器. #内建的哦.
-t <docroot>     指定用于内建web服务器的文档根目录<docroot>
-s               输出HTML语法高亮的源码
-v               输出PHP的版本号
-w               输出去掉注释和空格的源码
-z <file>        载入Zend扩展文件 <file>.

args...          传递给要运行的脚本的参数. 当第一个参数以`-`开始或者是脚本是从标准输入读取的时候,使用`--`参数

--ini            显示PHP的配置文件名

--rf <name>      显示关于函数 <name> 的信息.
--rc <name>      显示关于类 <name> 的信息.
--re <name>      显示关于扩展 <name> 的信息.
--rz <name>      显示关于Zend扩展 <name> 的信息.
--ri <name>      显示扩展 <name> 的配置信息.

再看一个问题

file或者file_get_contents时, https不行. 这个问题了.

  1. 查看一下设置.

    $w = stream_get_wrappers();
    echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
    echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
    echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
    echo 'wrappers: ', var_export($w);
    #说实话, 一般情况这个没有卵用.
    
  2. php.ini的设置不正确

    //需要修改php.ini
    extension=php_openssl.dll
    allow_url_fopen = On
    //但是更好的做法是, 用ini_set, 参考调试php那个blog.
    #然而失败了, extension必须在php.ini修改: http://php.net/manual/zh/ini.list.php
      extension	NULL	php.ini only
    
  3. 改了之后要确定是否生效

    php -i | grep "php_openssl.dll"
    php -i | grep "extension"
    php -i | grep "allow_url_fopen"
    #然后发现修改了php.ini.default没有鸟用.
    
  4. 有用的是下面这样的代码, 虽然有用, 然而是错误的解决办法

    #参考: http://stackoverflow.com/questions/26148701/file-get-contents-ssl-operation-failed-with-code-1-and-more
    http://php.net/manual/en/migration56.openssl.php
    $arrContextOptions=array(
        "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    );
    $url="https://www.baidu.com";
          $cons = file_get_contents($url,false, stream_context_create($arrContextOptions));
    
  5. 正解也不是很正.

    $arrContextOptions=array(
        "ssl"=>array(
            "cafile" => "/path/to/bundle/cacert.pem",//需要你的文件在这里.
            "verify_peer"=> true,
            "verify_peer_name"=> true,
        ),
    );
    

参考

  • http://php.net/manual/zh/function.file.php
  • http://php.net/manual/zh/function.file-get-contents.php