Created
May 12, 2017 18:39
-
-
Save masterfermin02/5f8db5b16490ca653f6b41a7e778c64f to your computer and use it in GitHub Desktop.
Http request help for php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function httpRequest($url) | |
{ | |
// make request | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POST,TRUE); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded; charset=UTF-8",)); | |
$output = curl_exec($ch); | |
// handle error; error output | |
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) { | |
var_dump($output); | |
} | |
curl_close($ch); | |
return $output; | |
} | |
/** | |
* Send a GET requst using cURL | |
* @param string $url to request | |
* @param array $get values to send | |
* @param array $options for cURL | |
* @return string | |
*/ | |
function curl_get($url, array $get = NULL, array $options = array()) | |
{ | |
$defaults = array( | |
CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get), | |
CURLOPT_HEADER => 0, | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_TIMEOUT => 4 | |
); | |
$ch = curl_init(); | |
curl_setopt_array($ch, ($options + $defaults)); | |
if( ! $result = curl_exec($ch)) | |
{ | |
trigger_error(curl_error($ch)); | |
} | |
curl_close($ch); | |
return $result; | |
} | |
/** | |
* Send a POST requst using cURL | |
* @param string $url to request | |
* @param array $post values to send | |
* @param array $options for cURL | |
* @return string | |
*/ | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment