/plugin/PBAPI/PBAPI/Request/fsockopen.php
PHP | 103 lines | 56 code | 12 blank | 35 comment | 9 complexity | 810f34b89f79c50cddd1c260c667af2c MD5 | raw file
1<?php 2use common\libraries\Path; 3/** 4 * Photobucket API 5 * Fluent interface for PHP5 6 * fsockopen request method 7 * 8 * @author Photobucket 9 * @package PBAPI 10 * @subpackage Request 11 * 12 * @copyright Copyright Copyright (c) 2008, Photobucket, Inc. 13 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 14 */ 15 16/** 17 * Load Request parent 18 */ 19require_once dirname(__FILE__) . '/../Request.php'; 20/** 21 * fsockopen request strategy 22 * requires ability to use fsockopen 23 * 24 * @package PBAPI 25 * @subpackage Request 26 */ 27class PBAPI_Request_fsockopen extends PBAPI_Request 28{ 29 30 /** 31 * Do actual request 32 * 33 * @param string $method 34 * @param string $uri 35 * @param array $params 36 * @return string 37 */ 38 protected function request($method, $uri, $params = array()) 39 { 40 $url = $this->preRequest($method, $uri, $params); 41 $parts = parse_url($url); 42 43 $len = 0; 44 $data = ''; 45 $resp = ''; 46 47 //open socket 48 if ($fp = @fsockopen($parts['host'], 80)) 49 { 50 51 //generate request headers 52 $path = (! empty($parts['path'])) ? $parts['path'] : ''; 53 $query = (! empty($parts['query'])) ? '?' . $parts['query'] : ''; 54 fputs($fp, "$method $path$query HTTP/1.1\n"); 55 fputs($fp, "Host: {$parts['host']}\n"); 56 fputs($fp, 'User-Agent: ' . __CLASS__ . "\n"); 57 58 //generate request headers for post 59 if ($method == 'POST') 60 { 61 if (self :: detectFileUploadParams($params)) 62 { 63 $boundary = uniqid('xx'); 64 $data = self :: multipartEncodeParams($this->oauth_request->getParameters(), $boundary); 65 fputs($fp, "Content-Type: multipart/form-data; boundary=$boundary\n"); 66 } 67 else 68 { 69 $data = $this->oauth_request->toPostdata(); 70 fputs($fp, "Content-type: application/x-www-form-urlencoded\n"); 71 } 72 73 $len = strlen($data); 74 fputs($fp, "Content-length: $len\n\n"); 75 fputs($fp, "$data\n"); 76 } 77 78 //put last newline to signal i'm done 79 fputs($fp, "\n"); 80 81 $headers = true; 82 while (! feof($fp)) 83 { 84 $line = fgets($fp); //get lines 85 if (trim($line) == '') 86 $headers = false; //empty line will signal that we're done with headers 87 else 88 if (! $headers) 89 $resp .= $line; //dont capture headers to response 90 } 91 92 //close socket 93 fclose($fp); 94 } 95 else 96 { 97 throw new PBAPI_Exception('FSOCKOPEN failed'); //todo exception 98 } 99 100 return $resp; 101 } 102 103}