Skip to content

Instantly share code, notes, and snippets.

@WGrape
Created January 22, 2022 14:10
Show Gist options
  • Save WGrape/417a22da0eaf1f5a809e1d35e56739ec to your computer and use it in GitHub Desktop.
Save WGrape/417a22da0eaf1f5a809e1d35e56739ec to your computer and use it in GitHub Desktop.
Send a simple http request by curl in PHP.
<?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