首頁 > 後端開發 > php教程 > 伺服器端PHP產生curl命令列

伺服器端PHP產生curl命令列

巴扎黑
發布: 2023-03-03 12:32:02
原創
1678 人瀏覽過

記錄請求到log,為了方便後續排查問題,在PHP裡呼叫一個方法,實作將curl串記錄下來即可方便復現請求。

<?php
/**
 * Author: xishizhaohua@qq.com
 * Date: 2015/11/2
 * 
 */
function getCurlCommand()
{
    try {
        if (php_sapi_name() == &#39;error cli&#39;){
            throw new Exception("cli");
        }
        $curlCommand = &#39;curl &#39;;
        $postData = $getData = &#39;&#39;;
        if($_GET) {
            $gets = http_build_query($_GET);
            $getData .= strpos($curlCommand, &#39;?&#39;) ? &#39;&&#39; . $gets : &#39;?&#39; . $gets;
        }
        if ($_SERVER[&#39;REQUEST_METHOD&#39;] == &#39;POST&#39; ) {
            $posts = http_build_query($_POST);
            $postData = &#39; -d "&#39; . $posts . &#39;"&#39;;
        }
        $path = isset($_SERVER[&#39;SCRIPT_NAME&#39;]) ? $_SERVER[&#39;SCRIPT_NAME&#39;] : $_SERVER[&#39;PHP_SELF&#39;];
        $curlCommand .= &#39;"&#39; . "http://{$_SERVER[&#39;HTTP_HOST&#39;]}" . $path . $getData . &#39;"&#39;;
        if ($postData) {
            $curlCommand .= $postData;
        }
        $headers = array();
        if (function_exists(&#39;getallheaders&#39;)) {
            $headers = getallheaders();
        } else {
            foreach ($_SERVER as $name => $value) {
                if (substr($name, 0, 5) == &#39;HTTP_&#39;) {
                    $headers[str_replace(&#39; &#39;, &#39;-&#39;, ucwords(strtolower(str_replace(&#39;_&#39;, &#39; &#39;, substr($name, 5)))))] = $value;
                }
            }
        }
        foreach ($headers as $key => $value) {
            if($key == &#39;Accept-Encoding&#39;)  $value = str_replace(&#39;gzip, &#39;,&#39;&#39;,$value);
            $curlCommand .= &#39; -H "&#39; . $key . &#39;:&#39; . $value . &#39;"&#39;;
        }
        return $curlCommand;
    } catch (Exception $e) {
        return $e->getMessage();
    }
}
echo getCurlCommand();
登入後複製

eg: 

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=shikiliu; email=xishizhaohua%40qq.com”
登入後複製

  

相關文章:

使用Curl指令檢視請求回應時間方法url

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板