/** * Configure and set the URL to use, along with any request parameters. * * @param resource $ch The cURL connection resource * @param string $host The host to send the request to * @param string $path The path of the request * @param string $method The method of the request (GET/POST) * @param array $params An array of request parameters to attach to the URL * @param array $options An array of options when setting the URL * @return boolean Whether or not the URL was set * @see modRestClient::request for parameter documentation. */ public function setUrl($ch, $host, $path, $method = 'GET', array $params = array(), array $options = array()) { $q = http_build_query($params); switch ($method) { case 'GET': $path .= (strpos($host, '?') === false ? '?' : '&') . $q; break; case 'POST': curl_setopt($ch, CURLOPT_POST, 1); $contentType = $this->modx->getOption('contentType', $options, 'xml'); switch ($contentType) { case 'json': $json = $this->modx->toJSON($params); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); break; case 'xml': curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); $xml = modRestArrayToXML::toXML($params, !empty($options['rootNode']) ? $options['rootNode'] : 'request'); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); break; default: curl_setopt($ch, CURLOPT_POSTFIELDS, $params); break; } break; } /* prevent invalid xhtml ampersands in request path */ $url = str_replace('&', '&', $host . $path); return curl_setopt($ch, CURLOPT_URL, $url); }
/** * Configure and set the URL to use, along with any request parameters. * * @param resource $ch The cURL connection resource * @param string $host The host to send the request to * @param string $path The path of the request * @param string $method The method of the request (GET/POST) * @param array $params An array of request parameters to attach to the URL * @param array $options An array of options when setting the URL * @return boolean Whether or not the URL was set * @see modRestClient::request for parameter documentation. */ public function setUrl($ch, $host, $path, $method = 'GET', array $params = array(), array $options = array()) { $q = http_build_query($params); switch ($method) { case 'GET': $path .= (strpos($host, '?') === false ? '?' : '&') . $q; break; case 'POST': curl_setopt($ch, CURLOPT_POST, 1); $contentType = $this->modx->getOption('contentType', $options, 'xml'); switch ($contentType) { case 'json': $json = $this->modx->toJSON($params); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); break; case 'xml': curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); $xml = modRestArrayToXML::toXML($params, !empty($options['rootNode']) ? $options['rootNode'] : 'request'); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); break; case 'string': curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); $string = implode('&', array_map(create_function('$v, $k', 'return $k . "=" . $v;'), $params, array_keys($params))); curl_setopt($ch, CURLOPT_POSTFIELDS, $string); break; default: curl_setopt($ch, CURLOPT_POSTFIELDS, $params); break; } break; } /* prevent invalid xhtml ampersands in request path and strip unnecessary ampersands from the end of the url */ $url = rtrim(str_replace('&', '&', $host . $path), '&'); return curl_setopt($ch, CURLOPT_URL, $url); }