Created
March 1, 2019 20:53
-
-
Save mttjohnson/a2ccb8127413d3afc3cf7df260dc20d8 to your computer and use it in GitHub Desktop.
matching/debugging a php curl request to using curl in bash in a script or cli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # These examples provide some simple and quick ways to experiment | |
| # with the differences between PHP curl requests and using the | |
| # curl command from a bash shell or script. The commands are arranged | |
| # so they can be easily copy and pasted into a shell for experimenting. | |
| # Use netcat listening locally for testing running netcat from one | |
| # shell and execute the client requests from a separate shell. This | |
| # can be useful to see newline characters and any additional characters | |
| # in the data submitted if it is represented the same by the curl implementation. | |
| # nc -l 8888 | |
| # change host address to http://localhost:8888 for sending requests | |
| # to the listening netcat process | |
| # PHP version of sending post data with curl | |
| echo "---------PHP curl use---------" | |
| set +H # disable history expansion | |
| PHP_CODE=$(cat <<'PHP_CODE' | |
| <?php | |
| # Echo requests back from httpbin.org | |
| $host = 'https://httpbin.org/post'; | |
| $post_content = <<<EOD | |
| MULTI-LINE | |
| line with leading spaces | |
| CONTENT | |
| line with trailing spaces | |
| tada! | |
| EOD; | |
| # Debugging Request - capturing verbose output | |
| # https://blog.kettle.io/debugging-curl-requests-in-php/ | |
| ob_start(); | |
| $out = fopen('php://output', 'w'); | |
| $headers = [ | |
| 'Content-Type:text/plain', | |
| 'X-MySpeacialHeader: LotsOfSpecialnessHere', | |
| 'X-SomeFlag: 1', | |
| ]; | |
| $ch = curl_init($host); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
| curl_setopt($ch, CURLOPT_POST, 1); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content); | |
| # Debugging Request - curl options | |
| curl_setopt($ch, CURLOPT_VERBOSE, true); | |
| curl_setopt($ch, CURLOPT_STDERR, $out); | |
| $response = curl_exec($ch); | |
| curl_close ($ch); | |
| # Debugging Request - close and capture output buffer | |
| fclose($out); | |
| $debug = ob_get_clean(); | |
| print_r($debug); | |
| echo "\n---------\n"; | |
| echo "Response\n"; | |
| echo $response . "\n"; | |
| PHP_CODE | |
| ) | |
| set -H # re-enable history expansion | |
| echo "${PHP_CODE}" | php | |
| # Bash version of sending post data with curl | |
| echo "---------Bash curl use---------" | |
| XML_PAYLOAD=$(cat <<XML_HEREDOC | |
| MULTI-LINE | |
| line with leading spaces | |
| CONTENT | |
| line with trailing spaces | |
| tada! | |
| XML_HEREDOC | |
| ) | |
| # 'echo -n' allows for not adding a newline character to end of contents | |
| # '--trace-ascii -' sends trace debug details to standard output STDOUT | |
| # '--header "User-Agent:"' removes the user-agent from the HTTP request | |
| # '--data-binary' prevents curl from manipulating line endings on text files | |
| # '-@' indicates curl should accept data from the standard input STDIN (what we are piping in) | |
| echo -n "${XML_PAYLOAD}" \ | |
| | curl -s \ | |
| -X POST \ | |
| --trace-ascii - \ | |
| --header 'User-Agent:' \ | |
| --header "Content-Type: text/plain" \ | |
| --header "X-MySpeacialHeader: LotsOfSpecialnessHere" \ | |
| --header "X-SomeFlag: 1" \ | |
| --data-binary @- \ | |
| "https://httpbin.org/post" \ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment