免责声明:php新手学生在这里工作非常努力。
我正在尝试构建一个与CallFire API交互的应用。
此时我想要实现的是与API建立通信并成功获得请求/响应周期。
我已在我的XAMPP本地服务器上安装了与Composer的Callfire SDK依赖项。我希望运行this非常基本的示例:它实例化API,发送请求并解析响应。
<?php
use CallFire\Api\Rest\Request;
require 'vendor/autoload.php';
$client = CallFire\Api\Client::Rest("<api-login>", "<api-password>", "Broadcast"); //I'm using my valid CallFire API keys here.
$request = new Request\QueryBroadcasts;
$response = $client->QueryBroadcasts($request); //LINE 10
$broadcasts = $client::response($response);
foreach($broadcasts as $broadcast) {
var_dump($broadcast);
}
我将此代码放在index.php文件中,当我在浏览器中运行时,我得到:
Notice: Trying to get property of non-object in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 39
Notice: Trying to get property of non-object in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 39
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Response type not recognized' in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php:50
Stack trace: #0 C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\AbstractClient.php(59): CallFire\Api\Rest\Response::fromXml(false)
#1 C:\xampp\htdocs\cfireapi1\index.php(10): CallFire\Api\Rest\AbstractClient::response(false)
#2 {main} thrown in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 50
AbstractClient.php中的第59行是
public static function response($data, $type = self::FORMAT_XML)
{
if (is_string($data) && strlen($data) === 0) {
return true; // Many operations return an empty string for success
}
switch ($type) {
case static::FORMAT_XML:
return AbstractResponse::fromXml($data); // LINE 59
case static::FORMAT_JSON:
return AbstractResponse::fromJson($data);
}
throw new InvalidArgumentException("Type must be 'xml' or 'json'");
}
Response.php中的第39行和第50行是
public static function fromXml($document)
{
$document = static::getXmlDocument($document);
// return print_r($document);
switch ($document->firstChild->nodeName) { // LINE 39 error for this line is Notice: Trying to get property of non-object
case 'r:ResourceList':
return Response\ResourceList::fromXml($document);
case 'r:Resource':
return Response\Resource::fromXml($document);
case 'r:ResourceReference':
return Response\ResourceReference::fromXml($document);
case 'r:ResourceException':
return Response\ResourceException::fromXml($document);
}
throw new UnexpectedValueException('Response type not recognized'); // LINE 50: this is what throws the fatal error exception
}
第10行在示例代码中突出显示。
所以我知道API正在发回一个带有未识别类型的响应。为什么,如果我真的使用SDK和文档中的一个基本示例?如何检查我的反应,看看它究竟是什么?我尝试在Response.php文件中使用r_print打印响应,但我得到了一个&#34; 1&#34;在浏览器中打印。这对我来说没有意义。是因为我在浏览器中运行它吗?
答案 0 :(得分:0)
已解决:以某种方式在localhost环境中运行程序导致了问题。我将应用程序上传到服务器后,一切正常。