2

我正在使用 parse.com 和用于与框架交互的 PHP 库仅在使用 MAMP 的 Mac 上为我工作,但在使用 XAMPP 或 WAMP 的 Windows 上,它每次都因相同的原因而失败。下面是每次连接解析时调用的函数。在 Windows 上运行时,$responseCode = 0 并且 $response 为 false。当我在我的 Mac 上运行它时,$responseCode = 200 和 $response 返回它假设的 json 数据。到底是怎么回事?为什么不同的平台会导致失败?

public function request($args){
        $isFile = false;
        $c = curl_init();
        curl_setopt($c, CURLOPT_TIMEOUT, 30);
        curl_setopt($c, CURLOPT_USERAGENT, 'parse.com-php-library/2.0');
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($c, CURLINFO_HEADER_OUT, true);
        if(substr($args['requestUrl'],0,5) == 'files'){
            curl_setopt($c, CURLOPT_HTTPHEADER, array(
                'Content-Type: '.$args['contentType'],
                'X-Parse-Application-Id: '.$this->_appid,
                'X-Parse-Master-Key: '.$this->_masterkey
            ));
            $isFile = true;
        }
        else if(substr($args['requestUrl'],0,5) == 'users' && isset($args['sessionToken'])){
            curl_setopt($c, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'X-Parse-Application-Id: '.$this->_appid,
                'X-Parse-REST-API-Key: '.$this->_restkey,
                'X-Parse-Session-Token: '.$args['sessionToken']
            ));
        }
        else{
            curl_setopt($c, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'X-Parse-Application-Id: '.$this->_appid,
                'X-Parse-REST-API-Key: '.$this->_restkey,
                'X-Parse-Master-Key: '.$this->_masterkey
            )); 
        }
        curl_setopt($c, CURLOPT_CUSTOMREQUEST, $args['method']);
        $url = $this->_parseurl . $args['requestUrl'];

        if($args['method'] == 'PUT' || $args['method'] == 'POST'){
            if($isFile){
                $postData = $args['data'];
            }
            else{
                $postData = json_encode($args['data']);
            }

            curl_setopt($c, CURLOPT_POSTFIELDS, $postData );
        }

        if($args['requestUrl'] == 'login'){
            $urlParams = http_build_query($args['data'], '', '&');
            $url = $url.'?'.$urlParams;
        }
        if(array_key_exists('urlParams',$args)){
            $urlParams = http_build_query($args['urlParams'], '', '&');
            $url = $url.'?'.$urlParams;
        }

        curl_setopt($c, CURLOPT_URL, $url);

        $response = curl_exec($c);
        $responseCode = curl_getinfo($c, CURLINFO_HTTP_CODE);

        $expectedCode = '200';
        if($args['method'] == 'POST' && substr($args['requestUrl'],0,4) != 'push'){
            $expectedCode = '201';
        }

        if($expectedCode != $responseCode){
            //BELOW HELPS WITH DEBUGGING
                    //echo "VAR DUMP:";
                    //var_dump($response);      
        }

        return $this->checkResponse($response,$responseCode,$expectedCode);
    }

private function checkResponse($response,$responseCode,$expectedCode){
        //TODO: Need to also check for response for a correct result from parse.com
        if($responseCode != $expectedCode) {

            $error = json_decode($response);
            $this->throwError($error->error,$error->code);
        }
        else{
            //check for empty return
            if($response == '{}'){
                return true;
            }
            else{
                return json_decode($response);
            }
        }
    }
}

错误:

[23-Jan-2013 19:57:45 UTC] PHP Notice:  Trying to get property of non-object in C:\wamp\www\firecom\parse\parse.php on line 180

[23-Jan-2013 19:57:45 UTC] PHP Notice:  Trying to get property of non-object in C:\wamp\www\firecom\parse\parse.php on line 180

[23-Jan-2013 19:57:45 UTC] PHP Fatal error:  Uncaught ParseLibraryException: [0]: thrown in C:\wamp\www\firecom\parse\parse.php on line 169
4

1 回答 1

2

代替:

$response = curl_exec($c);

和:

if( ! $response = curl_exec($c) ) {
    die(curl_error($c));
}

或者替换您喜欢的任何错误处理代码。

http://ca2.php.net/manual/en/function.curl-error.php

于 2013-01-23T21:40:48.463 回答