>  기사  >  백엔드 개발  >  서버 측 PHP는 컬 명령줄을 생성합니다.

서버 측 PHP는 컬 명령줄을 생성합니다.

巴扎黑
巴扎黑원래의
2017-03-23 15:25:431599검색

로그에 요청을 기록하려면 PHP에서 메서드를 호출하여 요청을 쉽게 재현할 수 있도록 컬 문자열을 기록하세요.

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

예:

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 명령을 사용하여 요청 응답 시간 보기

서버 측 PHP가 컬 명령줄을 생성합니다

Linux 시스템 컬 명령

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.