[英]Wordnik “Trying to get property of non-object” Error when trying getDefinitions
I'm trying to use the Wordnik PHP API and I'm having some trouble. 我正在尝试使用Wordnik PHP API,但遇到了一些麻烦。 I tried to use the getDefinitions method but it returns an error:
Notice: Trying to get property of non-object in C:\\xampp\\htdocs\\index.php on line 18
. 我尝试使用getDefinitions方法,但返回错误:
Notice: Trying to get property of non-object in C:\\xampp\\htdocs\\index.php on line 18
。
Here is the following code: 这是下面的代码:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post">
<input type="text" placeholder="First Word" name="word1">
<input type="submit" placeholder="Compare">
</form>
<?php
require('./wordnik/Swagger.php');
$APIKey = '342eac9900e703079b0050d5f7008eab962195189e75bfbcb';
$client = new APIClient($APIKey, 'http://api.wordnik.com/v4');
$word1 = $_POST['word1'];
$wordApi = new WordApi($client);
$word1 = $wordApi->getDefinitions($word1, null, null);
print $word1->text;
?>
</body>
</html>
I think your notice (not really an error in the php world) does not come from the $word1 = $wordApi->getDefinitions($word1, null, null);
我认为您的通知(实际上不是php世界中的错误)不是来自
$word1 = $wordApi->getDefinitions($word1, null, null);
but from print $word1->text;
但从
print $word1->text;
is it possible? 可能吗?
If you check the WorldApi class : 如果您检查WorldApi类:
https://github.com/wordnik/wordnik-php/blob/master/wordnik/WordApi.php#L182 https://github.com/wordnik/wordnik-php/blob/master/wordnik/WordApi.php#L138 https://github.com/wordnik/wordnik-php/blob/master/wordnik/WordApi.php#L182 https://github.com/wordnik/wordnik-php/blob/master/wordnik/WordApi.php#L138
You can see that getDefinitions(...)
return an array of Definition
or null. 您可以看到
getDefinitions(...)
返回一个Definition
数组或null。
One thing is sure, you cannot get the ->text
property from $word1
but from one of these indexes if the return is valid. 可以肯定的是,如果返回有效,则不能从
$word1
获得->text
属性,而是从这些索引之一中获得。 Try $word1[0]->text
尝试
$word1[0]->text
Anyway you should also handle the case where the return of getDefinitions(...)
return an empty array or null. 无论如何,您还应该处理以下情况:
getDefinitions(...)
返回返回空数组或null。
This sample code might help you: 此示例代码可能会帮助您:
apiUrl = 'http://api.wordnik.com/v4'
apiKey = 'YOURKEYHERE'
client = swagger.ApiClient(apiKey, apiUrl)
wordApi = WordApi.WordApi(client)
res = wordApi.getWord('cat')
res2 = wordApi.getDefinitions('cat')
assert res, 'null getWord result'
assert res.word == 'cat', 'word should be "cat"'
print res.word
print dir(res2[0])
print res2[0].partOfSpeech
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.