1

I have been trying to design a Signup Page using a tutorial in a Zend framework 2 Book .Problem is that I am unable to complete signup after running it as , clicking on register gives a socket error , which I have no clue.

    Zend\Http\Client\Adapter\Exception\RuntimeException
    File:
    C:\xampp\htdocs\ZF2AD\Chapter10\client\vendor\zendframework\zend-http\src\Client\Adapter\Socket.php:256
    Message:
    Unable to connect to zf2-api:80 . Error #0: stream_socket_client():unable to connect to zf2-api:80 (php_network_getaddresses: getaddrinfo failed: No such host is known. )

I am using Api to connect to database.Below is the code for my Apiclient.php

    <?php

    namespace Api\Client;

    use Zend\Http\Client;
    use Zend\Http\Request;
    use Zend\Json\Decoder as JsonDecoder;
    use Zend\Json\Json;
    use Zend\Log\Logger;
    use Zend\Log\Writer\Stream;

    /**
     * This client manages all the operations needed to interface with the
     * social network API
     *
     * @package default
     */
   class ApiClient {

/**
 * Holds the client we will reuse in this class
 *
 * @var Client
 */
protected static $client = null;

/**
 * Holds the endpoint urls
 *
 * @var string
 */
protected static $endpointHost = 'http://zf2-api';
protected static $endpointWall = '/api/wall/%s';
protected static $endpointFeeds = '/api/feeds/%s';
protected static $endpointSpecificFeed = '/api/feeds/%s/%d';
protected static $endpointUsers = '/api/users';
protected static $endpointGetUser = '/api/users/%s';
protected static $endpointUserLogin = '/api/users/login';

/**
 * Perform an API reqquest to retrieve the data of the wall
 * of an specific user on the social network
 *
 * @param string $username
 * @return Zend\Http\Response
 */
public static function getWall($username)
{
    $url = self::$endpointHost . sprintf(self::$endpointWall, $username);
    return self::doRequest($url);
}

/**
 * Perform an API request to post content on the wall of an specific user
 *
 * @param string $username 
 * @param array $data 
 * @return Zend\Http\Response
 */
public static function postWallContent($username, $data)
{
    $url = self::$endpointHost . sprintf(self::$endpointWall, $username);
    return self::doRequest($url, $data, Request::METHOD_POST);
}

/**
 * Perform an API request to get the list subscriptions of a username
 *
 * @param string $username 
 * @return Zend\Http\Response
 */
public static function getFeeds($username)
{
    $url = self::$endpointHost . sprintf(self::$endpointFeeds, $username);
    return self::doRequest($url);
}

/**
 * Perform an API request to add a new subscription
 *
 * @param string $username 
 * @param array $postData
 * @return Zend\Http\Response
 */
public static function addFeedSubscription($username, $postData)
{
    $url = self::$endpointHost . sprintf(self::$endpointFeeds, $username);
    return self::doRequest($url, $postData, Request::METHOD_POST);
}

/**
 * Perform an API request to remove a subscription
 *
 * @param string $username 
 * @param array $postData
 * @return Zend\Http\Response
 */
public static function removeFeedSubscription($username, $feedId)
{
    $url = self::$endpointHost . sprintf(self::$endpointSpecificFeed, $username, $feedId);
    return self::doRequest($url, null, Request::METHOD_DELETE);
}

/**
 * Perform an API request to add a new user
 *
 * @param array $postData
 * @return Zend\Http\Response
 */
public static function registerUser($postData)
{
    $url = self::$endpointHost . self::$endpointUsers;
    return self::doRequest($url, $postData, Request::METHOD_POST);
}

/**
 * Perform an API request to get the basic data of a user
 *
 * @param string $username
 * @return Zend\Http\Response
 */
public static function getUser($username)
{
    $url = self::$endpointHost . sprintf(self::$endpointGetUser, $username);
    return self::doRequest($url, null, Request::METHOD_GET);
}

/**
 * Perform an API request to authenticate a user
 *
 * @param array $postData Array containing username and password on their respective keys
 * @return Zend\Http\Response
 */
public static function authenticate($postData)
{
    $url = self::$endpointHost . self::$endpointUserLogin;
    return self::doRequest($url, $postData, Request::METHOD_POST);
}

/**
 * Create a new instance of the Client if we don't have it or 
 * return the one we already have to reuse
 *
 * @return Client
 */
protected static function getClientInstance()
{
    if (self::$client === null) {
        self::$client = new Client();
        self::$client->setEncType(Client::ENC_URLENCODED);
    }

    return self::$client;
}

/**
 * Perform a request to the API
 *
 * @param string $url
 * @param array $postData
 * @param Client $client
 * @return Zend\Http\Response
 * @author Christopher
 */
protected static function doRequest($url, array $postData = null, $method = Request::METHOD_GET)
{
    $client = self::getClientInstance();
    $client->setUri($url);
    $client->setMethod($method);

    if ($postData !== null) {
        $client->setParameterPost($postData);
    }

    $response = $client->send();

    if ($response->isSuccess()) {
        return JsonDecoder::decode($response->getBody(), Json::TYPE_ARRAY);
    } else {
        $logger = new Logger;
        $logger->addWriter(new Stream('data/logs/apiclient.log'));
        $logger->debug($response->getBody());
        return FALSE;
    }
}

}

Refining the error , I got to know that this is the main error in all errors

    stream_socket_client(): php_network_getaddresses: getaddrinfo failed: No such host is known. 

I have tried solutions provided in similar questions , but they didnt helped . If anyone knows about it please help.

Adi
  • 43
  • 10
  • I have checked similar looking question http://stackoverflow.com/questions/8210099/php-php-network-getaddresses-getaddrinfo-failed-no-such-host-is-known – Adi Dec 26 '15 at 08:47
  • 1
    It's failing to resolve `zf2-api`. Is this the hostname for your ZF2 app? If so the tutorial should have told you to add an entry to your hosts file. That's the step you've missed. – Tim Fountain Dec 26 '15 at 15:06
  • thats the name for my api developed for the app to connect to database – Adi Dec 28 '15 at 05:06
  • I have added entry to https-vhosts.conf which is in the folder xampp/apache/conf/extra – Adi Dec 28 '15 at 05:07
  • When I check Socket.php , at the path specified in the message , there is a block of code which is causing error if (!$this->socket) { $this->close(); throw new AdapterException\RuntimeException( sprintf( 'Unable to connect to %s:%d%s', $host, $port, ($error ? ' . Error #' . $error->getCode() . ': ' . $error->getMessage() : '') ), 0, $error ); } – Adi Dec 28 '15 at 05:27
  • `https-vhosts.conf` is not the file that needs changing. You need to add an entry to your system hosts file. On Windows I think it's something like `C:\Windows\System32\drivers\etc\hosts`. – Tim Fountain Dec 28 '15 at 10:33
  • @TimFountain : And how to add entry to hosts file? I am using localhost with xampp which doesnt have any ip settings like cpanel of a hosting server. – Adi Dec 28 '15 at 11:02
  • Edit the file - http://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/ If xampp is hosting your API as well, you want to add an entry to the host file to point that hostname at your local machine: `zf2-api 127.0.0.1`. – Tim Fountain Dec 28 '15 at 11:14
  • That means I just add an entry to the the last line of the file Just like 127.0.0.1 localhost ::1 localhost – Adi Dec 28 '15 at 11:46
  • Yup - sorry, the IP and hostname were the wrong way around in my comment above. – Tim Fountain Dec 28 '15 at 12:23
  • I did that , it doesnt shows any error but doesnt do anything and there is still nothing in database – Adi Dec 29 '15 at 05:38
  • @TimFountain I tried to edit the file and the error got away but on submitting sign up form , nothing happens , and nothing gets stored in database – Adi Jan 04 '16 at 05:59

0 Answers0