Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Simple GET request
- * getinfo("getRequest");
- *
- * Get request with headers
- * getinfo("getRequest", null, arrayHeaders);
- *
- * Post request
- * getinfo("url", "arrayPost[key => value] or arrayPost[value]");
- *
- * Post request with headers
- * getinfo("url", "arrayPost[key => value] or arrayPost[value]", arrayHeaders);
- */
- function getInfo (...$data) {
- if (count($data) >= 1 AND count($data) <= 3) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $data[0]);
- curl_setopt($ch, CURLOPT_FAILONERROR, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 3);
- if (isset($data[1]) AND !empty($data[1])) {
- if (is_array($data[1])) {
- $array = array();
- foreach ($data[1] as $key => $val)
- {
- if (!is_int($key)) {
- $array[] = $key."=".$val;
- }
- else {
- $arrayNotKey[] = $val;
- }
- }
- if (isset($arrayNotKey) AND count($arrayNotKey) > 0) {
- $postdata = $arrayNotKey[0];
- }
- else {
- $postdata = implode("&", $array);
- }
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
- }
- else {
- throw new Exception("2-ой аргумент функции ".__FUNCTION__." не является массивом.");
- }
- }
- if (isset($data[2]) AND !empty($data[2])) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $data[2]);
- }
- $data = curl_exec($ch);
- $info = curl_getinfo($ch);
- $info['data'] = $data;
- curl_close($ch);
- return $info;
- }
- else {
- throw new Exception("Не верное количество аргументов у функции ".__FUNCTION__.".");
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement