PHP: 开发PHP命令行脚本

windows环境。php

一、安装xampp、设置环境变量

官网下载安装。html

将php目录放入环境变量PATH中,在CMD中能够执行php命令。json

二、安装composer

去官网下载安装包安装,中间有一步是要选PHP执行程序。 选择PHP目录下的php.exe就好了。windows

三、示例1

编写test.php,composer

start
<?php
echo 'hello world!';

运行:curl

$ php test.php
start
hello world!

四、示例2

编写composer.json:ui

{
    "require": {
        "guzzlehttp/guzzle": "~6.0"
    }
}

运行 composer.bat installurl

如今目录结构以下:code

修改test.php:htm

<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
$client = new Client([
    'timeout'  => 2.0,
]);

$response = $client->get('http://www.baidu.com');
echo $response->getBody()->getContents();

运行:

$ php test.php

// ...
// ... www.baidu.com的首页内容

五、关于HTTPS请求

若是去请求 https://www.baidu.com ,可能会报错以下:

cURL error 60: SSL certificate problem: unable to get local issuer certificate (

http://stackoverflow.com/questions/29822686/curl-error-60-ssl-certificate-unable-to-get-local-issuer-certificatehttps://easywechat.org/zh-cn/docs/troubleshooting.html 有讨论。 https://curl.haxx.se/docs/sslcerts.html 是curl关于SSL的说明。

ca证书在这里: https://curl.haxx.se/docs/caextract.html

推荐作法:

https://curl.haxx.se/docs/caextract.html 下载一个ca证书(文本文件)

然户修改php.ini:

curl.cainfo = /path/to/downloaded/cacert.pem

或者修改

openssl.cafile= /path/to/downloaded/cacert.pem

注意,ini中分号;是注释。

相关文章
相关标签/搜索