Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function post ($data, $type = null) {
- if ( is_null($type) ) {
- $url = $this->rest_endpoint;
- }
- if ( !is_null($this->custom_post) ) {
- return call_user_func($this->custom_post, $url, $data);
- }
- if ( !preg_match("|http://(.*?)(/.*)|", $url, $matches) ) {
- die('There was some problem figuring out your endpoint');
- }
- if ( function_exists('curl_init') ) {
- // Has curl. Use it!
- $curl = curl_init($this->rest_endpoint);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($curl);
- curl_close($curl);
- } else {
- // Use sockets.
- foreach ( $data as $key => $value ) {
- $data[$key] = $key . '=' . urlencode($value);
- }
- $data = implode('&', $data);
- $fp = @pfsockopen($matches[1], 80);
- if (!$fp) {
- die('Could not connect to the web service');
- }
- fputs ($fp,'POST ' . $matches[2] . " HTTP/1.1\n");
- fputs ($fp,'Host: ' . $matches[1] . "\n");
- fputs ($fp,"Content-type: application/x-www-form-urlencoded\n");
- fputs ($fp,"Content-length: ".strlen($data)."\n");
- fputs ($fp,"Connection: close\r\n\r\n");
- fputs ($fp,$data . "\n\n");
- $response = "";
- while(!feof($fp)) {
- $response .= fgets($fp, 1024);
- }
- fclose ($fp);
- $chunked = false;
- $http_status = trim(substr($response, 0, strpos($response, "\n")));
- if ( $http_status != 'HTTP/1.1 200 OK' ) {
- die('The web service endpoint returned a "' . $http_status . '" response');
- }
- if ( strpos($response, 'Transfer-Encoding: chunked') !== false ) {
- $temp = trim(strstr($response, "\r\n\r\n"));
- $response = '';
- $length = trim(substr($temp, 0, strpos($temp, "\r")));
- while ( trim($temp) != "0" && ($length = trim(substr($temp, 0, strpos($temp, "\r")))) != "0" ) {
- $response .= trim(substr($temp, strlen($length)+2, hexdec($length)));
- $temp = trim(substr($temp, strlen($length) + 2 + hexdec($length)));
- }
- } elseif ( strpos($response, 'HTTP/1.1 200 OK') !== false ) {
- $response = trim(strstr($response, "\r\n\r\n"));
- }
- }
- return $response;
- }
Advertisement
Add Comment
Please, Sign In to add comment