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中分號;是註釋。

相關文章
相關標籤/搜索