本文实例讲述了php生成curl命令行的方法。分享给大家供大家参考,具体如下:
具体代码如下:
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();希望本文所述对大家php程序设计有所帮助。
易语言入门教程 CHM,介绍易语言的系统基本数据类型、常量表、运算符、位运算命令以及易语言支持库方面的问题,易语言所编写的程序运行时都需要加载易语言的支持库文件.表面上易语言的非独立编译所生成的EXE程序体积小巧.但事实上若想把软件发布出去给别人的电脑上使用.非独立编译将面临很多的问题.所以实际应用时应全部进行独立编译。
0
更多相关教程请访问 php编程从入门到精通全套视频教程
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号