chrome有一个屌屌的功能(Copy as cURL),PHP有没有已经实现的


如图:
d891b6b5fe88ab44496874c24602.png

把任意请求进行 Copy as cURL 后,就得到了形如如下所示的串:


 curl "http://example.com/article" -H "DNT: 1" -H "Accept-Encoding: gzip, deflate, sdch" -H "Accept-Language: en,zh-CN;q=0.8,zh;q=0.6,en-US;q=0.4" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Referer: http://example.com/" -H "Cookie: PHPSESSID=2nel57rojc12t2426vuan702s7" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" --compressed

如果有 post 数据的话,还可以加上 -d *** 等项(文件上传暂不考虑)。

现有一个服务器端项目,需要记录请求到 log ,为了方便后续排查问题,希望在 PHP 里调用一个方法,就能实现以上功能,将这个 curl 串记录下来即可方便复现请求。

有没有人用PHP写过,共享一份啊~~~

PS:
已经有人 swift 实现了一个


 //
//  NSURLRequest+cURL.swift
//
//  Created by Domagoj Tršan on 25/10/14.
//  Copyright (c) 2014 Domagoj Tršan. All rights reserved.
//  Licence: The MIT License (MIT)
//

import Foundation

extension NSURLRequest {

    /**
     *  Returns a cURL command for a request.
     *
     *  @return A String object that contains cURL command or nil if an URL is
     *  not properly initalized.
     */
    func cURL() -> String? {

        if let length = self.URL.absoluteString?.utf16Count {
            if (length == 0) {
                return nil
            }
        } else {
            return nil
        }

        let curlCommand = NSMutableString()
        curlCommand.appendString("curl")

        // append URL
        curlCommand.appendFormat(" '%@'", self.URL.absoluteString!)

        // append method if different from GET
        if("GET" != self.HTTPMethod) {
            curlCommand.appendFormat(" -X %@", self.HTTPMethod!)
        }

        // append headers
        let allHeadersFields = self.allHTTPHeaderFields as? [String: String]
        let allHeadersKeys = allHTTPHeaderFields?.keys.array as [String]
        let sortedHeadersKeys  = allHeadersKeys.sorted { $0 < $1 }
        for key in sortedHeadersKeys {
            curlCommand.appendFormat(" -H '%@: %@'",
                                      key, self.valueForHTTPHeaderField(key)!)
        }

        // append HTTP body
        let httpBody = self.HTTPBody
        if httpBody?.length > 0 {
            let httpBody = NSString(data: self.HTTPBody!,
                                    encoding: NSUTF8StringEncoding)
            let escapedHttpBody =
              NSURLRequest.escapeAllSingleQuotes(httpBody!)
            curlCommand.appendFormat(" --data '%@'", escapedHttpBody)
        }

        return String(curlCommand)
    }

    /**
     *  Escapes all single quotes for shell from a given string.
     *
     *  @param value The value to escape.
     *
     *  @return An escaped value.
     */
    class func escapeAllSingleQuotes(value: String) -> String {
        return
          value.stringByReplacingOccurrencesOfString("'", withString: "'\\''")
    }
}

chrome php curl console

小囧囧D雪 10 years, 7 months ago

Your Answer