Skip to content

Instantly share code, notes, and snippets.

@cahva
Created August 1, 2014 06:31
Show Gist options
  • Save cahva/132805b1741e4932b551 to your computer and use it in GitHub Desktop.
Save cahva/132805b1741e4932b551 to your computer and use it in GitHub Desktop.
curl_post helper
<?php
if (!function_exists('curl_post'))
{
function curl_post($url, array $post = NULL, array $options = array())
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
CURLOPT_POSTFIELDS => http_build_query($post)
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
}
@cahva
Copy link
Author

cahva commented Aug 1, 2014

Usage:

<?php

$arr = array(
    'foo1' => 'bar1',
    'foo2' => 'bar2'
    'foo3' => 'bar3'
);

$url = 'http://example.com/api'
curl_post($url,$arr);

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