Server-side PHP generates curl command line

巴扎黑
Release: 2023-03-03 12:32:02
Original
1639 people have browsed it

Record the request to the log. In order to facilitate subsequent troubleshooting, call a method in PHP to record the curl string to facilitate reproducing the request.

<?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();
Copy after login

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”
Copy after login

Related articles:

Use Curl command to check the request response time

Server-side PHP generates curl command line

Linux system curl command

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!