时间:2021-07-01 10:21:17 帮助过:36人阅读
如图:
把任意请求进行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: "'\\''")
}
}
如图:
把任意请求进行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: "'\\''")
}
}
收集到一个不错的,可供参考。
public function getCurlCommand() {
try {
if (php_sapi_name() == 'error cli') {
throw new Exception("cli");
}
$curl_command = 'curl ';
$post_data = $get_data = '';
if($_GET) {
$gets = http_build_query($_GET);
$get_data .= strpos($curl_command, '?') ? '&' . $gets : '?' . $gets;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$posts = http_build_query($_POST);
$post_data = ' -d "' . $posts . '"';
}
$path = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['PHP_SELF'];
$curl_command .= '"' . "http://{$_SERVER['HTTP_HOST']}" . $path . $get_data . '"';
if ($post_data) {
$curl_command .= $post_data;
}
$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);
$curl_command .= ' -H "' . $key . ':' . $value . '"';
}
return $curl_command;
} catch (Exception $e) {
return $e->getMessage();
}
}