php生成curl命令行的方法
本文實(shí)例講述了php生成curl命令行的方法。分享給大家供大家參考,具體如下:
示例:
curl "http://localhost/other/serverInfo.php?dd=ddd" -H "Host:localhost" -H "Connection:keep-alive" -H "Cache-Control:max-age=0" -H "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36" -H "DNT:1" -H "Accept-Encoding:deflate, sdch" -H "Accept-Language:zh-CN,zh;q=0.8,en;q=0.6" -H "Cookie:name=richie; email=richie@qq.com"
具體代碼如下:
function getCurlCommand()
{
try {
if (php_sapi_name() == 'error cli'){
throw new Exception("cli");
}
$curlCommand = 'curl ';
$postData = $getData = '';
if($_GET) {
$gets = http_build_query($_GET);
$getData .= strpos($curlCommand, '?') ? '&' . $gets : '?' . $gets;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
$posts = http_build_query($_POST);
$postData = ' -d "' . $posts . '"';
}
$path = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['PHP_SELF'];
$curlCommand .= '"' . "http://{$_SERVER['HTTP_HOST']}" . $path . $getData . '"';
if ($postData) {
$curlCommand .= $postData;
}
$headers = array();
if (function_exists('getallheaders')) {
$headers = getallheaders();
} else {
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
}
foreach ($headers as $key => $value) {
if($key == 'Accept-Encoding') $value = str_replace('gzip, ','',$value);
$curlCommand .= ' -H "' . $key . ':' . $value . '"';
}
return $curlCommand;
} catch (Exception $e) {
return $e->getMessage();
}
}
echo getCurlCommand();
希望本文所述對(duì)大家php程序設(shè)計(jì)有所幫助。
- PHP如何獲取命令行參數(shù)
- php命令行模式代碼實(shí)例詳解
- php web環(huán)境和命令行環(huán)境下查找php.ini的位置
- php根據(jù)命令行參數(shù)生成配置文件詳解
- php命令行寫shell實(shí)例詳解
- 命令行執(zhí)行php腳本中的$argv和$argc配置方法
- 利用PHP命令行模式采集股票趨勢(shì)信息
- 基于命令行執(zhí)行帶參數(shù)的php腳本并取得參數(shù)的方法
- 實(shí)例講解yii2.0在php命令行中運(yùn)行的步驟
- 如何通過Linux命令行使用和運(yùn)行PHP腳本
- PHP的命令行擴(kuò)展Readline相關(guān)函數(shù)的使用
相關(guān)文章
SESSION信息保存在哪個(gè)文件目錄下以及能夠用來保存什么類型的數(shù)據(jù)
session默認(rèn)是保存到c:\windows\temp目錄下,但是通過修改php.ini中的session.save_path值可以改變session的保存路徑2012-06-06
php中對(duì)xml讀取的相關(guān)函數(shù)的介紹一
php中對(duì)xml讀取的相關(guān)函數(shù)的介紹整理如下2008-06-06
PHP sleep()函數(shù), usleep()函數(shù)
這篇文章主要介紹了PHP sleep()函數(shù), usleep()函數(shù),需要的朋友可以參考下2016-08-08
php中的Base62類(適用于數(shù)值轉(zhuǎn)字符串)
以下是對(duì)php中Base62類的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08

