Skip to content

Instantly share code, notes, and snippets.

@Prid13
Created July 3, 2018 00:37
Show Gist options
  • Save Prid13/f5fa84a68b072abaac1414c11bbbc934 to your computer and use it in GitHub Desktop.
Save Prid13/f5fa84a68b072abaac1414c11bbbc934 to your computer and use it in GitHub Desktop.
cURL POST Request
<?php
// eksempel kode på INSERT til Candidate (legge til ny candidate)
$url = "https://api.recman.no/v2/post/candidate";
// lager en array med alle variablene vi sender med. Har kopiert fra eksempel til Insert på recman
$data = array(
{
"key": "YOUR_KEY",
"scope": "candidate",
"operation": "insert",
"data": {
"firstName": "Chris",
"lastName": "Reyes",
"email": "yugiohmaster@naruto.no",
"gender": "male"
}
}
);
// lager ny cURL objekt
$ch = curl_init($url);
// setter option for at vi skal ha resultat, til true (altså at den skal returnere noe)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// her velger vi hva slags HTTP-metode vi vil ha -- bruker POST her.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// sender med variablene i requesten
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
// kjører requesten til nettsiden, og lagrer responsen
$response = curl_exec($ch);
if (!$response){
// noe galt skjedde med requesten
echo "Error: Det skjedde noe galt!";
} else {
// printer ut responsen
print_r($response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment