public exec ( ) : Njasm\Soundcloud\Request\ResponseInterface | ||
return | Njasm\Soundcloud\Request\ResponseInterface |
public function exec()
{
$verb = strtoupper($this->resource->getVerb());
$this->buildDefaultHeaders();
$curlHandler = curl_init();
//curl_setopt_array($curlHandler, $this->options);
// workaround for issue njasm/soundcloud#28 on github.
// for some reason curl_setopt_array does not wanna work well with 7.0 on some PHP builds.
// needs further investigation.
foreach ($this->options as $index => $value) {
curl_setopt($curlHandler, $index, $value);
}
curl_setopt($curlHandler, CURLOPT_USERAGENT, $this->getUserAgent());
curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, $verb);
curl_setopt($curlHandler, CURLOPT_URL, $this->urlBuilder->getUrl());
if ($verb != 'GET') {
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $this->getBodyContent());
}
$response = curl_exec($curlHandler);
$info = curl_getinfo($curlHandler);
$errno = curl_errno($curlHandler);
$errorString = curl_error($curlHandler);
curl_close($curlHandler);
$this->options[CURLOPT_HTTPHEADER] = array();
return $this->factory->make('ResponseInterface', array($response, $info, $errno, $errorString));
}
public function testRequest() { $resource = new Resource('post', '/me', array('name' => 'John Doe')); $urlBuilder = new UrlBuilder($resource, '127', '0.0.1', 'http://'); // request Factory mock $reqFactoryMock = $this->getMock("Njasm\\Soundcloud\\Factory\\Factory", array('make')); $reqFactoryMock->expects($this->any())->method('make')->with($this->equalTo('ResponseInterface'))->will($this->returnCallback(function ($arg) { return new Response("HTTP/1.1 302 Found\nurl: http://127.0.0.1/index.php\r\n\r\n{\"status\": \"ok\"}", array('url' => 'http://127.0.0.1/index.php'), 0, "No Error"); })); $request = new Request($resource, $urlBuilder, $reqFactoryMock); $response = $request->exec(); $this->assertInstanceOf('Njasm\\Soundcloud\\Request\\ResponseInterface', $response); }