0

I usually use curl to call rest api's like this

$user_data = array("userName" => "myuser@mydomain.com" ,"password" => "mydomain123" );
$data = json_encode($user_data);
$headers = array('Accept: application/json', 'Content-Type: application/json',);
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, 'http://myursl.com/myapi/v1/Api.svc/something/someother');
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
print_r($response);

The above code is working fine with API and printing the expected response , When I do the same thing using Zend_Rest_client of Zend Framework

like this

$base_url = 'http://myursl.com/myapi/v1_0/Api.svc';
$endpoint = '/something/someother';
$user_data = array("userName" => "myuser@mydomain.com" ,"password" => "mydomain123" );
$client = new Zend_Rest_Client($base_url);
$response = $client->restPost($endpoint, $user_data);

I am getting 404 error like this

(Zend_Http_Response)#62 (5) { ["version":protected]=> string(3) "1.1" ["code":protected]=> int(404) ["message":protected]=> string(9) "Not Found"] }

Where actually I am wrong in implementing Zend Rest Client

Thanks in advance for responding to this post

Aravind.HU
  • 9,194
  • 5
  • 38
  • 50

1 Answers1

0

Although it might not be the finest solution I finally ended up inheriting Zend_Rest_Client and overriding _performPost() like this:

class My_Rest_Client extends Zend_Rest_Client {
    protected function _performPost($method, $data = null)
    {
        $client = self::getHttpClient();
        if (is_string($data)) {
            $client->setRawData($data, 'application/json');
        } elseif (is_array($data) || is_object($data)) {
            $client->setParameterPost((array) $data);
        }
        return $client->request($method);
    }
}

And then:

$rest = new My_Rest_Client('http://my.rest.url');
$rest->restPost('post-path', $data);
totas
  • 10,288
  • 6
  • 35
  • 32