PHP无法在json文件中调用一个对象

时间:2017-11-06 12:54:05

标签: php json api

我希望我的输出不是这样我想要的只有电话打印出这样的" 45656731"。如果我试试这个> echo $result->queryResult->EntityResults->Entity->Phone;它不起作用。

脚本

<?php 
require_once 'autoload.php';

$username = '*********************';
$password = '***************';
$authWsdl = 'https://webservices.autotask.net/atservices/1.5/atws.wsdl';
$opts = array('trace' => 1);
$client = new ATWS\Client($authWsdl, $opts);
$zoneInfo = $client->getZoneInfo($username);

$authOpts = array(
    'login' => $username,
    'password' => $password,
    'trace' => 1,   // Allows us to debug by getting the XML requests sent
);
$wsdl = str_replace('.asmx', '.wsdl', $zoneInfo->getZoneInfoResult->URL);
$client = new ATWS\Client($wsdl, $authOpts);

// Instantiate a Query object, designed to make complex
// queries simple.
$query = new ATWS\AutotaskObjects\Query('Contact');

$firstnameField = new ATWS\AutotaskObjects\QueryField('firstname');
$firstnameField->addExpression('Equals', 'Jeroen');

$query->addField($firstnameField);

// If you want to debug the XML produced by the Query object
// print($query->asXml());

// Print the results of the query
$json = ($client->query($query));
$result = json_encode($json);
echo $result;
?>

输出

{"queryResult":{"EntityResults":{"Entity":{"AccountID":193,"Active":1,"AdditionalAddressInformation":"","AddressLine":"Bernhardweg 30","AddressLine1":"","AlternatePhone":"","City":"***","Country":"Netherlands","CountryID":158,"CreateDate":"2016-10-22T09:52:03.807","EMailAddress":"****************","Extension":"","ExternalID":"","FaxNumber":"","FirstName":"Jeroen","LastActivityDate":"2016-10-22T09:52:03.807","LastModifiedDate":"2016-10-22T09:52:03.937","LastName":"Jansen","MiddleInitial":null,"MobilePhone":"*******","Note":"","Notification":true,"Phone":"45656731","RoomNumber":"","State":"","Title":"","ZipCode":"******","Fields":null,"UserDefinedFields":{"UserDefinedField":null},"id":30682895,"BulkEmailOptOut":false,"SurveyOptOut":false,"FacebookUrl":"","TwitterUrl":"","LinkedInUrl":"","PrimaryContact":false}},"EntityResultType":"contact","EntityReturnInfoResults":{"EntityReturnInfo":{"DatabaseAction":"None","DuplicateStatus":{"Found":false,"Ignored":false,"MatchInfo":null,"Fields":null,"UserDefinedFields":null},"EntityId":30682895,"Message":null,"Fields":null,"UserDefinedFields":null},"Fields":null,"UserDefinedFields":null},"Errors":{"ATWSError":null,"Fields":null,"UserDefinedFields":null},"ReturnCode":1}}

2 个答案:

答案 0 :(得分:0)

第一次运行此脚本并将代码中的#cats替换为您要查找的内容时,它并不像第一次看起来那么难。 希望你能开始工作!

function getTweets($hash_tag) {

$url = 'http://search.twitter.com/search.atom?q='.urlencode($hash_tag) ;
echo "<p>Connecting to <strong>$url</strong> ...</p>";
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec ($ch);
curl_close ($ch);

//If you want to see the response from Autotask, uncomment this next part out:
//echo "<p>Response:</p>";
//echo "<pre>".htmlspecialchars($xml)."</pre>";

$affected = 0;
$twelement = new SimpleXMLElement($xml);
foreach ($twelement->entry as $entry) {
    $text = trim($entry->title);
    $author = trim($entry->author->name);
    $time = strtotime($entry->published);
    $id = $entry->id;
    echo "<p>Tweet from ".$author.": <strong>".$text."</strong>  <em>Posted ".date('n/j/y g:i a',$time)."</em></p>";
}

return true ;
}

getTweets('#cats');
祝你好运!

答案 1 :(得分:0)

在将结果编码为json字符串之前,请使用它来检索电话号码:

$json = ($client->query($query));
$phone = $json->queryResult->EntityResults->Entity->Phone;
echo $phone;