我正在利用 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
最佳答案
替换:
$response = curl_exec($c);
与:
if( ! $response = curl_exec($c) ) {
die(curl_error($c));
}
或者替换您喜欢的任何错误处理代码。
关于curl - PHP 脚本可以在 Mac 上运行,但不能在 Windows 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14489856/