Last active
February 24, 2018 09:14
-
-
Save ranaroussi/d8abf071d80091cd2303ecc3b6f6cdf9 to your computer and use it in GitHub Desktop.
curl "requests" (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 | |
/** | |
* PHP implementation of Pyton's requests module | |
* Copyright 2017-2018 Ran Aroussi | |
* | |
* Licensed under the GNU Lesser General Public License, v3.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* https://www.gnu.org/licenses/lgpl-3.0.en.html | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
class Requests | |
{ | |
public static function get($url, $params = null, $final_url = false, $interface = null) | |
{ | |
return Requests::curl('GET', $url, $params, $final_url, $interface); | |
} | |
public static function post($url, $params = null, $final_url = false, $interface = null) | |
{ | |
return Requests::curl('POST', $url, $params, $final_url, $interface); | |
} | |
private static function curl($method, $url, $params = null, $final_url = false, $interface = null) | |
{ | |
$post_string = ''; | |
if (is_array($params) && !empty($params)) { | |
foreach ($params as $k => $v) { | |
$post_string .= $k.'='.$v.'&'; | |
} | |
} | |
if ($method == 'POST') { | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_POST, ($post_string)?1:0); | |
if ($post_string) { | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); | |
} | |
} else { | |
$url .= (strstr($url, '?')) ? '&' : '?'; | |
$url .= $post_string; | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_POST, 0); | |
} | |
if ($interface) { | |
curl_setopt($ch, CURLOPT_INTERFACE, $interface); | |
} | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 120); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 1); // RETURN HTTP HEADERS? | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN THE CONTENTS OF THE CALL? | |
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64;"); | |
$curl_data = curl_exec($ch); | |
if ($final_url) { | |
$curl_data = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // get last URL followed | |
} | |
curl_close($ch); | |
return Requests::objectify($curl_data); | |
} | |
private static function objectify($data) | |
{ | |
$res = array( | |
'reason' => 'OK', | |
'status' => '200 OK', | |
'status_code' => 200, | |
'headers' => '', | |
'text' => '', | |
'content' => '' | |
); | |
list($res['headers'], $res['text']) = explode("\r\n\r\n", $data, 2); | |
// redirect headers? | |
if (strpos($res['text'], 'HTTP/1.1') !== FALSE) { | |
list($headers, $res['text']) = explode("\r\n\r\n", $res['text'], 2); | |
$res['headers'] = [$res['headers'], $headers]; | |
} else { | |
$res['headers'] = [$res['headers']]; | |
} | |
$code = explode("\n", $res['headers'][count($res['headers'])-1])[0]; | |
$status = explode(' ', $code, 2); | |
$res['status'] = array_pop($status); | |
$res['status_code'] = explode(' ', $res['status'])[0]; | |
$status = explode(' ', $code, 3); | |
$res['reason'] = array_pop($status); | |
$res['content'] = $res['text']; | |
try { | |
$res['json'] = json_decode($res['text']); | |
} catch (Exception $e) { | |
$res['json'] = (object)array(error=>'Not a JSON format'); | |
} | |
return (object)$res; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment