After you have made any request Copy as cURL
, you get a string that looks like this:
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
If there is post
data, you can also add -d ***
items (file upload temporarily not considered).
Existing a server-side project, need to record the request to log
, in order to facilitate follow-up troubleshooting, I hope in the PHP
call a method, you can achieve the above functions, this curl
string record can be easily reproduced request.
Has anyone written in PHP, share a copy AH ~ ~ ~
Ps:
Already someone has swift
implemented a:
nsurlrequest+curl.swift////Created by Domagoj Tršan on 25/10/14.//Copyright (c) Domagoj Tršan. All rights reserved.//Licence:the mit License (mit)//import foundationextension nsurlrequest {/** * Returns a CURL command for a request. * * @return A String object that contains CURL command or nil if the URL is * not properly initalized. */func-CURL ()-String? {if let is length = self. Url.absolutestring?. Utf16count {if (length = = 0) {return nil}} else {return nil } Let Curlcommand = Nsmutablestring () curlcommand.appendstring ("curl")//Append URL Curl Command.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 {$ < $} for key in Sortedheader Skeys {Curlcommand.appendformat ("-H '%@:%@ '", Key, Self.valueforhttphea Derfield (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)}/** * Es Capes all quotes for Shell from a given string. * * @param value of the value to escape. * * @return an escaped value. */class Func escapeallsinglequotes (value:string), String { Return value.stringbyreplacingoccurrencesofstring ("'", withstring: ' \ \ ' ")}}
Reply content:
After you have made any request Copy as cURL
, you get a string that looks like this:
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
If there is post
data, you can also add -d ***
items (file upload temporarily not considered).
Existing a server-side project, need to record the request to log
, in order to facilitate follow-up troubleshooting, I hope in the PHP
call a method, you can achieve the above functions, this curl
string record can be easily reproduced request.
Has anyone written in PHP, share a copy AH ~ ~ ~
PS:
Someone has implemented a Swift
:
nsurlrequest+curl.swift////Created by Domagoj Tršan on 25/10/14.//Copyright (c) Domagoj Tršan. All rights reserved.//Licence:the mit License (mit)//import foundationextension nsurlrequest {/** * Returns a CURL command for a request. * * @return A String object that contains CURL command or nil if the URL is * not properly initalized. */func-CURL ()-String? {if let is length = self. Url.absolutestring?. Utf16count {if (length = = 0) {return nil}} else {return nil } Let Curlcommand = Nsmutablestring () curlcommand.appendstring ("curl")//Append URL Curl Command.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 {$ < $} for key in Sortedheader Skeys {Curlcommand.appendformat ("-H '%@:%@ '", Key, Self.valueforhttphea Derfield (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)}/** * Es Capes all quotes for Shell from a given string. * * @param value of the value to escape. * * @return an escaped value. */class Func escapeallsinglequotes (value:string), String { Return value.stringbyreplacingoccurrencesofstring ("'", withstring: ' \ \ ' ")}}
Collected to a good, for reference.
Public Function Getcurlcommand () {try {if (php_sapi_name () = = ' Error cli ') {throw new Exception ("C Li "); } $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 (); }}