GetResponse API显示错误,但工作JSON


GetResponse API showing error but working JSON

我有代码从GetResponse添加一个联系到一个活动。该脚本正在工作,因为它将联系人添加到GetResponse,然而,它一直显示这2个错误,并在我的服务器上留下一个错误日志。如果可以,请帮我修正这两个错误。

下面是我得到的2个错误....

警告:在autoresponders/getresponse/jsonrpclient .php的第242行中,输入:" (ASCII=1) state=0 "中的意外字符

严格的标准:只有变量应该在autoresponders/getresponse/add.php的第29行通过引用传递联系人添加

代码如下:

. php:

<?php
# Demonstrates how to add new contact to campaign.
# JSON::RPC module is required
# available at http://github.com/GetResponse/DevZone/blob/master/API/lib/jsonRPCClient.php
require_once 'jsonRPCClient.php';
# your API key is available at
# https://app.getresponse.com/my_api_key.html
$api_key = 'MY KEY IS HERE JUST LEFT OUT';
# API 2.x URL
$api_url = 'http://api2.getresponse.com';
# initialize JSON-RPC client
$client = new jsonRPCClient($api_url);
# find campaign name
$campaigns = $client->get_campaigns(
    $api_key,
    array (
        # find by name literally
        'name' => array ( 'EQUALS' => 'mytestcampaignrr' )
    )
);
# uncomment following line to preview Response
# print_r($campaigns);
# because there can be only one campaign of this name
# first key is the CAMPAIGN_ID required by next method
# (this ID is constant and should be cached for future use)
$CAMPAIGN_ID = array_pop(array_keys($campaigns));
# add contact to the campaign
$result = $client->add_contact(
    $api_key,
    array (
        # identifier of 'test' campaign
        'campaign'  => $CAMPAIGN_ID,
        # basic info
        'name'      => 'Randy Thomas',
        'email'     => 'example@example.com'
    )
);
# uncomment following line to preview Response
# print_r($result);
print("Contact added'n");
# Pawel Pabian http://implix.com
?>

jsonRPCClient.php

<?php
/**
 * jsonRPCClient.php
 *
 * Written using the JSON RPC specification -
 * http://json-rpc.org/wiki/specification
 *
 * @author Kacper Rowinski <krowinski@implix.com>
 * http://implix.com
 */
class jsonRPCClient
{
    protected $url = null, $is_debug = false, $parameters_structure = 'array';
    /**
     * Default options for curl
     *
     * @var array
     */
    protected $curl_options = array(
        CURLOPT_CONNECTTIMEOUT => 8,
        CURLOPT_TIMEOUT => 8
    );
    /**
     * Http error statuses
     *
     * Source: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
     *
     * @var array
     */
    private $httpErrors = array(
        400 => '400 Bad Request',
        401 => '401 Unauthorized',
        403 => '403 Forbidden',
        404 => '404 Not Found',
        405 => '405 Method Not Allowed',
        406 => '406 Not Acceptable',
        408 => '408 Request Timeout',
        500 => '500 Internal Server Error',
        502 => '502 Bad Gateway',
        503 => '503 Service Unavailable'
    );
    /**
     * Takes the connection parameter and checks for extentions
     *
     * @param string $pUrl - url name like http://example.com/
     */
    public function __construct($pUrl)
    {
        $this->validate(false === extension_loaded('curl'), 'The curl extension must be loaded for using this class!');
        $this->validate(false === extension_loaded('json'), 'The json extension must be loaded for using this class!');
        // set an url to connect to
        $this->url = $pUrl;
    }
    /**
     * Return http error message
     *
     * @param $pErrorNumber
     *
     * @return string|null
     */
    private function getHttpErrorMessage($pErrorNumber)
    {
        return isset($this->httpErrors[$pErrorNumber]) ? $this->httpErrors[$pErrorNumber] : null;
    }
    /**
     * Set debug mode
     *
     * @param boolean $pIsDebug
     *
     * @return jsonRPCClient
     */
    public function setDebug($pIsDebug)
    {
        $this->is_debug = !empty($pIsDebug);
        return $this;
    }
    /**
     * Set structure to use for parameters
     *
     * @param string $pParametersStructure 'array' or 'object'
     *
     * @throws UnexpectedValueException
     * @return jsonRPCClient
     */
    public function setParametersStructure($pParametersStructure)
    {
        if (in_array($pParametersStructure, array('array', 'object')))
        {
            $this->parameters_structure = $pParametersStructure;
        }
        else
        {
            throw new UnexpectedValueException('Invalid parameters structure type.');
        }
        return $this;
    }
    /**
     * Set extra options for curl connection
     *
     * @param array $pOptionsArray
     *
     * @throws InvalidArgumentException
     * @return jsonRPCClient
     */
    public function setCurlOptions($pOptionsArray)
    {
        if (is_array($pOptionsArray))
        {
            $this->curl_options = $pOptionsArray + $this->curl_options;
        }
        else
        {
            throw new InvalidArgumentException('Invalid options type.');
        }
        return $this;
    }
    /**
     * Performs a request and gets the results
     *
     * @param string $pMethod - A String containing the name of the method to be invoked.
     * @param array  $pParams - An Array of objects to pass as arguments to the method.
     *
     * @throws RuntimeException
     * @return array
     */
    public function __call($pMethod, $pParams)
    {
        static $requestId = 0;
        // generating uniuqe id per process
        $requestId++;
        // check if given params are correct
        $this->validate(false === is_scalar($pMethod), 'Method name has no scalar value');
        $this->validate(false === is_array($pParams), 'Params must be given as array');
        // send params as an object or an array
        $pParams = ($this->parameters_structure == 'object') ? $pParams[0] : array_values($pParams);
        // Request (method invocation)
        $request = json_encode(array('jsonrpc' => '2.0', 'method' => $pMethod, 'params' => $pParams, 'id' => $requestId));
        // if is_debug mode is true then add url and request to is_debug
        $this->debug('Url: ' . $this->url . "'r'n", false);
        $this->debug('Request: ' . $request . "'r'n", false);
        $responseMessage = $this->getResponse($request);
        // if is_debug mode is true then add response to is_debug and display it
        $this->debug('Response: ' . $responseMessage . "'r'n", true);
        // decode and create array ( can be object, just set to false )
        $responseDecoded = json_decode($responseMessage, true);
        // check if decoding json generated any errors
        $jsonErrorMsg = $this->getJsonLastErrorMsg();
        $this->validate( !is_null($jsonErrorMsg), $jsonErrorMsg . ': ' . $responseMessage);
        // check if response is correct
        $this->validate(empty($responseDecoded['id']), 'Invalid response data structure: ' . $responseMessage);
        $this->validate($responseDecoded['id'] != $requestId, 'Request id: ' . $requestId . ' is different from Response id: ' . $responseDecoded['id']);
        if (isset($responseDecoded['error']))
        {
            $errorMessage = 'Request have return error: ' . $responseDecoded['error']['message'] . '; ' . "'n" .
                'Request: ' . $request . '; ';
            if (isset($responseDecoded['error']['data']))
            {
                $errorMessage .= "'n" . 'Error data: ' . $responseDecoded['error']['data'];
            }
            $this->validate( !is_null($responseDecoded['error']), $errorMessage);
        }
        return $responseDecoded['result'];
    }
    /**
     * When the method invocation completes, the service must reply with a response.
     * The response is a single object serialized using JSON
     *
     * @param string $pRequest
     *
     * @throws RuntimeException
     * @return string
     */
    protected function & getResponse(&$pRequest)
    {
        // do the actual connection
        $ch = curl_init();
        if ( !$ch)
        {
            throw new RuntimeException('Could''t initialize a cURL session');
        }
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $pRequest);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ( !curl_setopt_array($ch, $this->curl_options))
        {
            throw new RuntimeException('Error while setting curl options');
        }
        // send the request
        $response = curl_exec($ch);
        // check http status code
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (isset($this->httpErrors[$httpCode]))
        {
            throw new RuntimeException('Response Http Error - ' . $this->httpErrors[$httpCode]);
        }
        // check for curl error
        if (0 < curl_errno($ch))
        {
            throw new RuntimeException('Unable to connect to '.$this->url . ' Error: ' . curl_error($ch));
        }
        // close the connection
        curl_close($ch);
        return $response;
    }
    /**
     * Throws exception if validation is failed
     *
     * @param bool   $pFailed
     * @param string $pErrMsg
     *
     * @throws RuntimeException
     */
    protected function validate($pFailed, $pErrMsg)
    {
        if ($pFailed)
        {
            throw new RuntimeException($pErrMsg);
        }
    }
    /**
     * For is_debug and performance stats
     *
     * @param string $pAdd
     * @param bool   $pShow
     */
    protected function debug($pAdd, $pShow = false)
    {
        static $debug, $startTime;
        // is_debug off return
        if (false === $this->is_debug)
        {
            return;
        }
        // add
        $debug .= $pAdd;
        // get starttime
        $startTime = empty($startTime) ? array_sum(explode(' ', microtime())) : $startTime;
        if (true === $pShow and !empty($debug))
        {
            // get endtime
            $endTime = array_sum(explode(' ', microtime()));
            // performance summary
            $debug .= 'Request time: ' . round($endTime - $startTime, 3) . ' s Memory usage: ' . round(memory_get_usage() / 1024) . " kb'r'n";
            echo nl2br($debug);
            // send output imidiately
            flush();
            // clean static
            $debug = $startTime = null;
        }
    }
    /**
     * Getting JSON last error message
     * Function json_last_error_msg exists from PHP 5.5
     *
     * @return string
     */
    function getJsonLastErrorMsg()
    {
        if (!function_exists('json_last_error_msg'))
        {
            function json_last_error_msg()
            {
                static $errors = array(
                    JSON_ERROR_NONE           => 'No error',
                    JSON_ERROR_DEPTH          => 'Maximum stack depth exceeded',
                    JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
                    JSON_ERROR_CTRL_CHAR      => 'Unexpected control character found',
                    JSON_ERROR_SYNTAX         => 'Syntax error',
                    JSON_ERROR_UTF8           => 'Malformed UTF-8 characters, possibly incorrectly encoded'
                );
                $error = json_last_error();
                return array_key_exists($error, $errors) ? $errors[$error] : 'Unknown error (' . $error . ')';
            }
        }
        // Fix PHP 5.2 error caused by missing json_last_error function
        if (function_exists('json_last_error'))
        {
            return json_last_error() ? json_last_error_msg() : null;
        }
        else
        {
            return null;
        }
    }
}
?>

我找到问题了。线:

$CAMPAIGN_ID = array_pop(array_keys($campaigns));

必须这样分割:

$CAMPAIGN_ID = array_keys($campaigns);
$CAMPAIGN_ID = array_pop($CAMPAIGN_ID);

至于Json脚本中的错误,我只是删除了以下几行,它可以正常工作:

if (true === $pShow and !empty($debug))
        {
            // get endtime
            $endTime = array_sum(explode(' ', microtime()));
            // performance summary
            $debug .= 'Request time: ' . round($endTime - $startTime, 3) . ' s Memory usage: ' . round(memory_get_usage() / 1024) . " kb'r'n";
            echo nl2br($debug);
            // send output imidiately
            flush();
            // clean static
            $debug = $startTime = null;
        }

我不知道如果这是一个真正的修复Json脚本,它可能会导致一些其他错误,但至少它的工作没有显示错误消息,这就是我想要的