我正在使用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
答案 0 :(得分:2)
替换:
$response = curl_exec($c);
使用:
if( ! $response = curl_exec($c) ) {
die(curl_error($c));
}
或者替换您喜欢的任何错误处理代码。