Example 1: http post request php curl
$post = [
'teste' => $_POST['teste']
];
httpPost('url.com', $post);
// function
function httpPost($url, $data)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Example 2: php curl get
PHP cURL GET Request
A GET request retrieves data from a server. This can be a website’s HTML, an API response or other resources.
<?php
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, 'https://hostname.tld/phone-list');
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$phoneList = curl_exec($cURLConnection);
curl_close($cURLConnection);
$jsonArrayResponse - json_decode($phoneList);
Example 3: php curl example
function getUrl($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Example 4: curl in php
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => [
item1 => 'value',
item2 => 'value2'
]
]);
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
Example 5: curl php example
function makeAPICall($url){
$handle = curl_init();
// Set the url
curl_setopt($handle, CURLOPT_URL, $url);
// Set the result output to be a string.
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($handle);
curl_close($handle);
echo $output;
return $output;
}
Example 6: curl php
// Initialize Curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://coinmarketcap.com/"); // set live website where data from
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // default
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // default
$content = curl_exec($curl);
preg_match_all('!<p color="text3" class="sc-AxhUy bzeXdk coin-item-symbol" font-size="1">(.*?)</p>!', $content, $matches);
var_dump($matches);