噗浪 OAuth API with PHP 7.1.8

第一篇開發筆記,總之先丟這篇上來,文章列表那些的以後再說~
只是個包含試誤過程的記錄,別當教學看,應該也沒人看得下去就是了XD

前情提要:忘記之前遇到什麼各種問題,索性整個主機重裝到最新版,故作業系統是CentOS Linux 7,PHP 是7.1.8
現在是找了噗浪官方的 PHP API link 要試著弄機器人,github 上也明確寫了需要準備 "pecl_http, for http_build_url(), http_*."
試了一下果然沒有,只好來裝啦~

首先要裝 pear,原本裝 go-pear/phar 路徑設錯,重裝,結果一直沒寫進 /usr/bin
結果執行 pear_old 確認版本居然是新的...不確定之前是什麼情況,總之是不能執行 pear 才裝的
索性直接把 /usr/bin/pear_old 拿掉 _old 看來也能用
之後 pear upgrade 試試看
照指示先更新 channel => sudo pear channel-update pear.php.net
sudo pear upgrade => sudo pear upgrade --force

原本以為是 pear install pecl_http 結果是要 sudo pecl install pecl_http
經過各種確認動作...就略過吧,我也不太記得了,總之:

sudo pecl install pecl_http
pecl 無法 parse XML
解法:link
=> vi `which pecl` =>拿掉最後一行的 -n
問題變成 phpize,要裝 php-devel
發現可以用 yum install php71w-pear 裝 pear......
改用 yum 裝,php71w-pear、php71w-devel
sudo pecl install pecl_http => no acceptable C compiler found in $PATH 等等
sudo yum install gcc 跑了一大串後 where to find zlib [/usr] :
libcurl、libevent、libicu、libidn2、libidn、libidnkit2、libidnkit 都直接 enter
結果 configure: error: please install and enable pecl/raphf
在 php.ini 加上 extension=propro.so 和 extension=raphf.so

/tmp/pear/install/pecl_http/src/php_http_encoding.h:16:18: fatal error: zlib.h: No such file or directory
 #include <zlib.h>
                  ^
compilation terminated.
make: *** [src/php_http.lo] Error 1
ERROR: `make' failed

安裝 sudo yum install zlib-devel 後再試...更大一串...成功了0.0
configuration option "php_ini" is not set to php.ini location
You should add "extension=http.so" to php.ini

php -v 測試:
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/http.so' - /usr/lib64/php/modules/http.so: undefined symbol: uidna_IDNToASCII in Unknown on line 0
extension 順序確認過,補裝 sudo pecl install libidn2、libicu、libevent 試試也沒用

後來找到這篇 link
重點:"In case anyone faces similar situation, there seems to be a bug on pecl_http-3.1.0 for Centos 7. I end up just using the previous stable version of pecl_http-3.0.1" [躺]
之前都只看 pecl_http 最下面的 Dependencies 沒注意看版本列表,不然早該想到換版本試試
移除改裝3.0.1版,解決~~趕緊 run run 看......"pecl_http 2+ won't provide http_ functions any more." 幹...

所以程式也要修改成 pecl_http 2+ 的方式,改一改遇到下面問題:
http\Client 找不到 drivers,也就是找不到預設的 curl,phpinfo 看是未 compiled
=> sudo yum install libcrul-devel 後重裝 pecl_http-3.0.1 解決
看來要 compile 就要裝對應的 devel
另外問搜尋路徑時改成 /usr/lib64 的話會問比較少,看來應該是要這樣才有找到東西,所以前面補裝的應該沒用,要也是要裝 devel 版,有需要再移掉吧

到這邊總算成功使用噗浪 API 了
修改了第175行的 http_build_url 那段,改成:

	$build_url = new \http\Url(
	    array(
			"scheme" => $parsed['scheme'],
			"host"   => $parsed['host'],
			)
		);
	$realm = $build_url->toString();

	if ($is_form_encoded) {
		$request = new http\Client\Request('POST',$uri, $headers);
		$request->getBody()->append (new http\QueryString($parameters));
		$client = new http\Client;
		$client->enqueue($request)->send();
		$response = $client->getResponse($request);
		$bod = $response->getBody();
	    return $bod;

還有 public function __set 的 if (isset($value)) 裡面改成:

		$parsed = parse_url($this->props[$prop]);
		$build_url = new \http\Url(
			array(
			    "scheme" => $parsed['scheme'],
			    "host"   => $parsed['host'],
			    "path"   => $parsed['path']
			    )
			);
		$this->props['normalized_url'] = 
		    $this->normalized_url = $build_url->toString();

其他還有 http_get、http_head、http_request 可能需要改,等用到再說
pecl_http 2+ 官方文件 link

裝 gcc 時就看到這篇了 link
不過沒注意到 curl-devel 因為之前看 lib 明明就有 curl,phpinfo 也有看到就忽略了
結果是最後的關鍵,這裡備份一下:
{
Install PECL_HTTP on CentOS
In order to use handy HTTP extension in your PHP code you need to install it first, as it doesn't come with PHP core installation.

Below are the steps to install it on CentOS:
Install PHP Pear if not installed yet:
yum install php-pear
Install GCC if not installed yet:
yum install gcc
Install cURL if not installed yet:
yum install curl-devel
Otherwise you'll get the following message while installing PECL_HTTP:
configure: error: could not find curl/curl.h

Start the main installation:
/usr/bin/pecl install pecl_http
You can use default values when answering installation questions

Add the following line to /etc/php.ini file:
extension=http.so
Restart your apache server so the extension can be loaded:
httpd -k restart
You should now be able to use it. hope I saved you some time :)
}
link 這篇應該也是參考過上面那篇,也可以看一下
主要差在 pcre-devel,不過我沒裝不知道有什麼差