Last active
November 28, 2019 11:55
-
-
Save fmtarif/f47478a5e112ab086bf2854084cf96b3 to your computer and use it in GitHub Desktop.
#php minimal curl post call
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
| <?php | |
| $query = array( | |
| 'foo' => 'bar', | |
| ); | |
| $url = 'https://example.com/api/url'; | |
| $curl = curl_init(); | |
| $curlOptions = array( | |
| CURLOPT_RETURNTRANSFER => TRUE, | |
| CURLOPT_URL => $url, | |
| CURLOPT_POSTFIELDS => http_build_query($query), // CURLOPT_POSTFIELDS sets the method to POST automatically | |
| CURLOPT_VERBOSE => true, | |
| CURLOPT_FAILONERROR => true, | |
| ); | |
| curl_setopt_array($curl, $curlOptions); | |
| $jsonResponse = json_decode(curl_exec($curl), TRUE); | |
| if ($error_no = curl_errno($curl)) { | |
| $error_msg = curl_error($curl); | |
| throw new Exception("Curl error no: $error_no - error: $error_msg"); | |
| } | |
| curl_close($curl); | |
| print_r($jsonResponse); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment