php生成curl命令行的方法
程序员文章站
2022-09-09 12:00:04
本文实例讲述了php生成curl命令行的方法。分享给大家供大家参考,具体如下:
示例:
curl "http://localhost/other/serverinfo....
本文实例讲述了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();
希望本文所述对大家php程序设计有所帮助。