Created
January 22, 2022 14:10
-
-
Save WGrape/417a22da0eaf1f5a809e1d35e56739ec to your computer and use it in GitHub Desktop.
Send a simple http request by curl in PHP.
This file contains 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 | |
function curlRequest($url, $method = "GET", $data = "", $userPassword = "") | |
{ | |
$httpHeader = array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data) | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_USERPWD, $userPassword); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
$response = curl_exec($ch); | |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment