// GET Request
<?php
$url = 'http://api.com/api/test'
$ch = curl_init($url); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $data = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
echo ($httpcode>=1) ? $data : 'no data';
?>
// POST Request for JSON
$url = 'http://api.com/api/post'; $pdata = array("key1"=>"val1","key2"=>"val2"); $ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($pdata)); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); curl_setopt($ch, CURLOPT_TIMEOUT, 20); $curldata = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
// POST Request from your Form will be like this :curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/application/x-www-form-urlencoded","content:text/html"));
// CURL POST OR GET from same domain or origin or server.use url loop-back address by using http://127.0.0.1/for ex : if your curl url is as mentioned above : http://api.com/api/post replace this with loop-back address,
like this http://127.0.0.1/api/post