Skip to content

Instantly share code, notes, and snippets.

@anytizer
Last active December 29, 2015 19:29
Show Gist options
  • Select an option

  • Save anytizer/7718066 to your computer and use it in GitHub Desktop.

Select an option

Save anytizer/7718066 to your computer and use it in GitHub Desktop.
Web contents via CURL call
<?php
/**
* Opens an URL with POST parameter and keeps quiet. Useful to post the data.
* @todo Make sure curl is initialized on the server, otherwise, the sytem may not perform
* @todo Handle $parameters if it is a multi dimentional array
*/
function web_contents($url='http://', $parameters_associative=array(), $fetch_data=false)
{
$fetch_data = ($fetch_data===true);
$parameters_key_value = array();
foreach($parameters_associative as $key => $value)
{
// assumes single dimensional
$parameters_key_value[$key]="{$key}=".($value);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'User Agent');
curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $parameters_key_value));
/**
* Do we need the data back or just need to inform the URL
* In case of API information, just send a notification to the server and do NOT wait for the completion.
* When data is required, it may take longer time to complete receiving the total output.
*/
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $fetch_data);
curl_setopt($ch, CURLOPT_TIMEOUT, $fetch_data?100:10);
$response_html = curl_exec($ch);
curl_close($ch);
return $response_html;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment