我有一个问题,当我在下面的函数中执行返回$ record 时,我在邮递员漂亮的视图中获得有效的json,但是如果我尝试print_r($ record),则结尾处为null JSON。
然后,由于某种原因,当我尝试 json_decode 时,输出总是 null 。
如果有人对为什么会发生这种情况有任何想法,我非常感谢您的投入。
谢谢
function webhook_listener($request_data){
$client = new ZohoCRMClient('Contacts', 'API key Here');
$parameters = $request_data->get_params();
if( !isset( $parameters['contactId'] ) || empty($parameters['contactId']) ){
file_put_contents(plugin_dir_path( __FILE__ ).'invalid.txt', 'No parameter found');
return array( 'error' => 'no_parameter_given' );
}else{
$companyid = $parameters['contactId'];
//file_put_contents(plugin_dir_path( __FILE__ ).'crm.txt', $parameters);
$record = $client->getRecordById()->id($companyid)->request();
$record = json_decode($record);
$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
throw new RuntimeException("JSON decode error: $error");
}
echo $company = $record[1]['data']['Company'];
}
}
JSON:
{"1":{"index":1,"data":{"CONTACTID":"3345923000000546002","SMOWNERID":"3345923000000158021","Contact Owner":"Frank Rosa","First Name":"Administrator","Last Name":"Ian","Email":"poojarajan3ellc@gmail.com","Created Time":"2018-09-19 14:32:35","Modified Time":"2018-09-20 02:48:51","Full Name":"Administrator Ian","Description":"Equity and Empowerment through Education. It is the Mission of 3e LLC to promote equity and empowerment for all students through engaging professional development for educators and parents, one-on-one coaching for teacher efficacy, and mentoring services for youth to promote enrichment and success. For the empowered, we offer editing, transcribing, and ghostwriting services to ensure your voice is heard.","Last Activity Time":"2018-09-20 02:48:51","Instagram Url":"http:\/\/www.instagram.com\/3e_llc","Company":"3ELLC","Website":"https:\/\/www.3ellc.org","Phone_1":"(727) 420-1050","Full Address":"2152 Arcadia Rd, Holiday, FL 34690, USA","Facebook Url":"http:\/\/www.facebook.com\/3eLLC\/","Logo Url":"https:\/\/dev.energypages.com\/wp-content\/uploads\/2018\/05\/header-logo-57.png","Twitter Url":"http:\/\/www.twitter.com\/3e_llc","Membership Level":"Basic","Select Service":"Technology","User ID":"347"}}}
var_dump输出:
array(1) {
[1]=>
object(CristianPontes\ZohoCRMClient\Response\Record)#2068 (2) {
["index"]=>
int(1)
["data"]=>
array(22) {
["CONTACTID"]=>
string(19) "3345923000000546002"
["SMOWNERID"]=>
string(19) "3345923000000158021"
["Contact Owner"]=>
string(10) "Frank Rosa"
["First Name"]=>
string(13) "Administrator"
["Last Name"]=>
string(3) "Ian"
["Email"]=>
string(25) "poojarajan3ellc@gmail.com"
["Created Time"]=>
string(19) "2018-09-19 14:32:35"
["Modified Time"]=>
string(19) "2018-09-20 02:48:51"
["Full Name"]=>
string(17) "Administrator Ian"
["Description"]=>
string(407) "Equity and Empowerment through Education. It is the Mission of 3e LLC to promote equity and empowerment for all students through engaging professional development for educators and parents, one-on-one coaching for teacher efficacy, and mentoring services for youth to promote enrichment and success. For the empowered, we offer editing, transcribing, and ghostwriting services to ensure your voice is heard."
["Last Activity Time"]=>
string(19) "2018-09-20 02:48:51"
["Instagram Url"]=>
string(31) "http://www.instagram.com/3e_llc"
["Company"]=>
string(5) "3ELLC"
["Website"]=>
string(21) "https://www.3ellc.org"
["Phone_1"]=>
string(14) "(727) 420-1050"
["Full Address"]=>
string(39) "2152 Arcadia Rd, Holiday, FL 34690, USA"
["Facebook Url"]=>
string(30) "http://www.facebook.com/3eLLC/"
["Logo Url"]=>
string(73) "https://dev.energypages.com/wp-content/uploads/2018/05/header-logo-57.png"
["Twitter Url"]=>
string(29) "http://www.twitter.com/3e_llc"
["Membership Level"]=>
string(5) "Basic"
["Select Service"]=>
string(10) "Technology"
["User ID"]=>
string(3) "347"
}
}
}
null
上面的输出是邮递员的原始视图。在漂亮的视图中,我得到了这个错误(意外的“ a”)
答案 0 :(得分:0)
关于如何解析jSON的所有答案都是正确的。但是我在使用php库链接到zoho时,该链接以不同的方式处理数据,因此我必须对来自API的响应进行foreach,然后对数据进行json_encode()和json_decode()来获取值。
我正在使用的图书馆:https://github.com/cristianpontes/zoho-crm-client-php
foreach($record as $records){
$payload = $records->getData();
$payload = json_encode($payload);
$payload = json_decode($payload, true);
$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
throw new RuntimeException("JSON decode error: $error");
}
$company = $payload['Company'];
}
感谢所有为@ miken32提供帮助的人