错误#0:stream_socket_client():无法连接到zf2-api:80(php_network_getaddresses:getaddrinfo失败:没有这样的主机已知。)

时间:2015-12-26 08:21:29

标签: php api zend-framework

我一直在尝试使用Zend框架2书中的教程来设计一个注册页。问题是我在运行它后无法完成注册,点击寄存器会出现套接字错误,我不知道。

    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. )

我正在使用Api连接到database.Below是我的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;
    }
}

}

改进错误,我知道这是所有错误中的主要错误

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

我尝试过在类似问题中提供的解决方案,但他们没有帮助。如果有人知道,请帮助。

0 个答案:

没有答案
相关问题