/** * builds the data one would send in a POST request * * */ public function to_postdata() { //we can probably take this level of indirection out // the function urlencodeArray seemed more like a utily/ static sort of function // so i moved it there $out = OAuthUtil::encodeUrlEncodedArray($this->parameters); return $out; }
/** * this function will make a raw HTTP requests using PHP's cURL * * //maybe? make* functions should always be called with a try(){}catch(){} */ private function makeHttpRequest($method, $url, $qParams, $headers = array(), $bodyContent = NULL) { $datetime = new DateTime(); $datetime = $datetime->format(DATE_ATOM); if (self::$MS_DUMP_REQUESTS) { $dump = "\r\n"; $dump .= '::makeHttpRequest::@' . $datetime . "\r\n"; $dump .= "_____________________________________________\r\n"; $dump .= '::reqUrl::\'' . $method . '\' ' . $url . "\r\n"; $dump .= '::reqParams::' . OAuthUtil::encodeUrlEncodedArray($qParams) . "\r\n"; $dump .= '::custom headers::' . "\r\n"; foreach ($headers as $key => $value) { $dump .= $key . ': ' . $value . "\r\n"; } $dump .= "\r\n"; $dump .= '::bodyContent::' . "\r\n"; $dump .= $bodyContent; self::dump($dump); } //this url, may or may not have signed params on it already, //if it does, we may not want more params //hmmm what about the body content being signed? if (!self::isSupportedMethod($method)) { //raise error, unsupported request method } //not sure if this is needed yet $url_bits = parse_url($url); $req_url = $url_bits['path']; if (empty($bodycontent)) { //if we are doing a GET, the query params need to be in the request url //maybe for DELETE too? //still not sure about this if ($url_bits['query']) { $req_url .= '?' . $url_bits['query']; } } //init curl $ch = curl_init(); /** *@link http://us.php.net/manual/en/function.curl-setopt.php */ //something related to CURLOP_SSL_VERIFYPEER, and validating certs from local a local path if (defined("CURL_CA_BUNDLE_PATH")) { curl_setopt($ch, CURLOPT_CAINFO, CURL_CA_BUNDLE_PATH); } //sets the url we are going to make a request from //this is the full $url from the function call curl_setopt($ch, CURLOPT_URL, $url); //The number of seconds to wait whilst trying to connect. Use 0 to wait indefinitely curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); //The maximum number of seconds to allow cURL functions to execute. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //setup the headers $h = array(); foreach ($headers as $k => $v) { $h[] = $k . ": " . $v; } curl_setopt($ch, CURLOPT_HEADER, true); //ok, now we need to worry about POST, and PUTs switch ($method) { case 'GET': //we do not need to do anything break; case 'POST': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); //The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value. curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyContent); //does this work with UTF-8 chars? //$h[] = 'Content-Length: ' . strlen($bodyContent); break; case 'PUT': //if we are going to put a file, //this needs to be done differently //this is designed for small url encoded strings curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); //The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value. curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyContent); //$h[] = 'Content-Length: ' . strlen($bodyContent); break; case 'DELETE': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; } self::dump("\r\n" . '::Sent Headers::' . "\r\n" . implode("\r\n", $h)); curl_setopt($ch, CURLOPT_HTTPHEADER, array(implode("\r\n", $h))); $myspace_response = curl_exec($ch); $responseBody = ''; $responseHeader = ''; list($responseHeader, $responseBody) = explode("\r\n\r\n", $myspace_response, 2); $responseStatus = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); $responseContentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); //if a content type was found if ($responseContentType) { $responseContentType = preg_replace("/;.*/", "", $responseContentType); // strip off charset } $response = array('contentType' => $responseContentType, 'status' => $responseStatus, 'headers' => $responseHeader, 'body' => $responseBody, 'raw' => $myspace_response); return $response; }