/** * Create a SoftLayer API XML-RPC Client * * Retrieve a new XmlRpcClient object for a specific SoftLayer API * service using either the class' constants API_USER and API_KEY or a * custom username and API key for authentication. Provide an optional id * value if you wish to instantiate a particular SoftLayer API object. * * @param string $serviceName The name of the SoftLayer API service you wish to query * @param int $id An optional object id if you're instantiating a particular SoftLayer API object. Setting an id defines this client's initialization parameter header. * @param string $username An optional API username if you wish to bypass XmlRpcClient's built-in username. * @param string $username An optional API key if you wish to bypass XmlRpcClient's built-in API key. * @param string $endpointUrl The API endpoint base URL you wish to connect to. Set this to XmlRpcClient::API_PRIVATE_ENDPOINT to connect via SoftLayer's private network. * @return XmlRpcClient */ public static function getClient($serviceName, $id = null, $username = null, $apiKey = null, $endpointUrl = null) { $serviceName = trim($serviceName); $id = trim($id); $username = trim($username); $apiKey = trim($apiKey); if ($serviceName == null) { throw new \Exception('Please provide a SoftLayer API service name.'); } $client = new self(); /* * Default to use the public network API endpoint, otherwise use the * endpoint defined in API_PUBLIC_ENDPOINT, otherwise use the one * provided by the user. */ if (isset($endpointUrl)) { $endpointUrl = trim($endpointUrl); if ($endpointUrl == null) { throw new \Exception('Please provide a valid API endpoint.'); } $client->_endpointUrl = $endpointUrl; } elseif (self::API_BASE_URL != null) { $client->_endpointUrl = self::API_BASE_URL; } else { $client->_endpointUrl = self::API_PUBLIC_ENDPOINT; } if ($username != null && $apiKey != null) { $client->setAuthentication($username, $apiKey); } else { $client->setAuthentication(self::API_USER, self::API_KEY); } $client->_serviceName = $serviceName; if ($id != null) { $client->setInitParameter($id); } return $client; }
/** * Creates a HttpTransport from a URI * * Supports http and https schemes, port-, path- and auth-definitions * If the port is ommitted 80 and 443 are used respectively. * If a username but no password is given, and empty password is used. * If a https URI is given, the provided SslOptions (with a fallback to * the default SslOptions) are used. * * @param string $url * @param SslOptions|null $sslOptions * * @return HttpTransport */ public static function fromUrl($url, SslOptions $sslOptions = null) { $parsed = parse_url($url); // check it's a valid URL if (false === $parsed || !isset($parsed['host']) || !isset($parsed['scheme'])) { throw new \InvalidArgumentException("{$url} is not a valid URL"); } // check it's http or https $scheme = strtolower($parsed['scheme']); if (!in_array($scheme, array('http', 'https'))) { throw new \InvalidArgumentException("{$url} is not a valid http/https URL"); } // setup defaults $defaults = array('port' => 80, 'path' => '', 'user' => null, 'pass' => ''); // change some defaults for https if ($scheme == 'https') { $sslOptions = $sslOptions ?: new SslOptions(); $defaults['port'] = 443; } // merge defaults and real data and build transport $parsed = array_merge($defaults, $parsed); $transport = new self($parsed['host'], $parsed['port'], $parsed['path'], $sslOptions); // add optional authentication if ($parsed['user']) { $transport->setAuthentication($parsed['user'], $parsed['pass']); } return $transport; }