Skip to content

Instantly share code, notes, and snippets.

@bakyeono
Last active February 22, 2016 07:32
Show Gist options
  • Save bakyeono/e8943a719f3ef5257be7 to your computer and use it in GitHub Desktop.
Save bakyeono/e8943a719f3ef5257be7 to your computer and use it in GitHub Desktop.
CURL GET & POST in PHP
<?php
function curl_get($url) {
$curl = curl_init();
curl_setopt_array($curl, array (
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'useragent'
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
function curl_post($url, $params) {
$curl = curl_init();
curl_setopt_array($curl, array (
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'useragent'
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
?>
@bakyeono
Copy link
Author

bakyeono commented Feb 3, 2016

usage

$url = "http://some-rest-api.com/api";
$params = "param1=value1&param2=value2";
$response1 = curl_get($url . '?' . $params);
$response2 = curl_post($url, $params);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment