首先,我想为我的新手状态道歉。我一直在研究人们在多维数组中回应一个值的其他问题,并且我的代码存在问题。我正在访问API,并最终希望将值放在表中。这就是我先做的事情:
<?php
$ch = curl_init('https://apps.net-results.com/api/v2/rpc/server.php?Controller=Contact');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'user:pass');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode(
array(
'id' => uniqid(),
'method' => 'getMultiple',
'jsonrpc' => '2.0',
'params' => array(
'offset' => 0,
'limit' => 50, // 10, 25, or 50
'order_by' => 'contact_email_address', //'contact_email_address' or 'contact_id'
'order_dir' => 'ASC', //'ASC' or 'DESC'
)
)
)
);
$strResponse = curl_exec($ch);
$strCurlError = curl_error($ch);
if (!empty($strCurlError)) {
//handle curl error
echo "Curl Error<br />$strCurlError<br />";
} else {
//check for bad user/pass
if ($strResponse == "HTTP/1.0 401 Unauthorized: Your username name and/or password are invalid.") {
//handle login error
echo "Error<br />$strResponse<br />";
} else {
//successful call, check for api success or error
$objResponse = json_decode($strResponse);
if (property_exists($objResponse, 'error') && !is_null($objResponse->error)) {
//handle error
$intErrorCode = $objResponse->error->code;
$strMessage = $objResponse->error->message;
$strData = $objResponse->error->data;
echo "Error<br />Code: $intErrorCode<br />Message: $strMessage<br />Data: $strData<br />";
} else {
//handle success
echo "Success<br />";
$objResult = $objResponse->result;
$intTotalRecords = $objResult->totalRecords;
echo "Total Records: $intTotalRecords<br />";
$arrContacts = $objResult->results;
echo "<pre>";
print_r($arrContacts);
echo "<pre/>";
}
}
}
返回:
总记录:8
Array
(
[0] => stdClass Object
(
[contact_id] => 306247082
[email_address] =>
[first_name] =>
[last_name] =>
[title] =>
[bounce_status] =>
[unsubscribe_status] =>
[account_id] => 33494139
[account] => Comcast Ip Services L.l.c.
[address_1] =>
[address_2] =>
[country] => United States
[state] => Pennsylvania
[city] => Pittsburgh
[postalcode] =>
[work_phone] =>
[home_phone] =>
[fax] =>
[create_date] => 2015-09-15T14:38:03+00:00
)
[1] => stdClass Object
(
[contact_id] => 306830523
[email_address] =>
[first_name] =>
[last_name] =>
[title] =>
[bounce_status] =>
[unsubscribe_status] =>
[account_id] => 33495425
[account] => COMCAST CABLE COMMUNICATIONS
[address_1] =>
[address_2] =>
[country] => United States
[state] => Colorado
[city] => Boulder
[postalcode] =>
[work_phone] =>
[home_phone] =>
[fax] =>
[create_date] => 2015-09-16T15:46:43+00:00
)
[2] => stdClass Object
(
[contact_id] => 306999302
[email_address] =>
[first_name] =>
[last_name] =>
[title] =>
[bounce_status] =>
[unsubscribe_status] =>
[account_id] => 33536778
[account] => LIQUID WEB INC.
[address_1] =>
[address_2] =>
[country] => United States
[state] => Oregon
[city] => Portland
[postalcode] =>
[work_phone] =>
[home_phone] =>
[fax] =>
[create_date] => 2015-09-16T22:59:14+00:00
)
[3] => stdClass Object
(
[contact_id] => 304689645
[email_address] =>
[first_name] =>
[last_name] =>
[title] =>
[bounce_status] =>
[unsubscribe_status] =>
[account_id] =>
[account] =>
[address_1] =>
[address_2] =>
[country] => -
[state] => -
[city] => -
[postalcode] =>
[lead_owner] =>
[work_phone] =>
[home_phone] =>
[fax] =>
[create_date] => 2015-09-11T19:53:21+00:00
)
[4] => stdClass Object
(
[contact_id] => 305707747
[email_address] =>
[first_name] =>
[last_name] =>
[title] =>
[bounce_status] =>
[unsubscribe_status] =>
[account_id] => 33456585
[account] =>
[address_1] =>
[address_2] =>
[country] => -
[state] => -
[city] => -
[postalcode] =>
[work_phone] =>
[home_phone] =>
[fax] =>
[create_date] => 2015-09-14T14:55:18+00:00
)
[5] => stdClass Object
(
[contact_id] => 306389764
[email_address] =>
[first_name] =>
[last_name] =>
[title] =>
[bounce_status] =>
[unsubscribe_status] =>
[account_id] => 33456581
[account] => -
[address_1] =>
[address_2] =>
[country] => -
[state] => -
[city] => -
[postalcode] =>
[work_phone] =>
[home_phone] =>
[fax] =>
[create_date] => 2015-09-15T19:11:49+00:00
)
[6] => stdClass Object
(
[contact_id] => 305707370
[email_address] =>
[first_name] =>
[last_name] =>
[title] =>
[bounce_status] =>
[unsubscribe_status] =>
[account_id] => 33456585
[account] =>
[address_1] =>
[address_2] =>
[country] => -
[state] => -
[city] => -
[postalcode] =>
[work_phone] =>
[home_phone] =>
[fax] =>
[create_date] => 2015-09-14T14:54:20+00:00
)
[7] => stdClass Object
(
[contact_id] => 305707549
[email_address] =>
[first_name] =>
[last_name] =>
[title] =>
[bounce_status] =>
[unsubscribe_status] =>
[account_id] => 33456585
[account] =>
[address_1] =>
[address_2] =>
[country] => -
[state] => -
[city] => -
[postalcode] =>
[work_phone] =>
[home_phone] =>
[fax] =>
[create_date] => 2015-09-14T14:54:52+00:00
)
)
我继续尝试添加:
echo "Some string: {$arrContacts[0][contact_id]}";
这不会返回任何内容。我有遗失的声明吗?再次感谢您的帮助。