I have a problem with a json decoded array using cURL. I am gathering data from Leadfeeder to get recent companies that visited our site. I have a custom start and end date and I wish to return all the companies that visited in the day. I can do this but it returns a json array. I am only able to access one company. However I need to access the entire array
below is an example of the code
$url = "https://api.leadfeeder.com/accounts/youraccount/visits?start_date=".date("2018-02-04")."&end_date=".date("Y-m-d").""; // Captcha plugin url
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Authorization: Token token=Token-here'));
$result = (string)curl_exec($ch);
echo "<pre>";
$leadfeeder = json_decode($result, true);
#$lead_id = $leadfeeder['data'][1]['attributes']['lead_id'];
foreach ($leadfeeder as $r) {
echo $r['data'];
}
When I run the above I get the following error
Notice: Undefined index: data in /opt/lampp/htdocs/leadfeeder.php on
line 28
Notice: Undefined index: data in /opt/lampp/htdocs/leadfeeder.php on
line 28
This is an example of the output
Array
(
[data] => Array
(
[0] => Array
(
[id] => 86b8e468-0af7-11e8-8738-11129da81150
[type] => visits
[attributes] => Array
(
[source] => en.wikipedia.org
[medium] => referral
[referring_url] =>
[page_depth] => 1
[visit_route] => Array
(
[0] => Array
(
[hostname] =>
[page_path] => /
[previous_page_path] => (entrance)
[time_on_page] => 0
[page_title] =>
[page_url] =>
[display_page_name] =>
)
)
[keyword] =>
[visit_length] => 0
[started_at] => 2018-02-05T23:51:00.000Z
[date] => 2018-02-05
[hour] => 23
[lead_id] => 02e01b12-2c9b-11e5-b645-3df00765f195_uM2sktjSDJNrnkRHg49WRe
)
)
Related
I am trying to call an API on this URL where I am expecting to insert a data. The API keeps returning "district id is required" even though I am passing the parameters completely. I am stuck since 3 days. Please refer to the screenshot below:
Postman Screenshot
API link I am calling here: https://hrtaskapi.dhwaniris.in/index.php/api/beneficiary/child-profile-create
Collection link: https://www.postman.com/collections/66c45e6146e3d7cbe41b
Required Header: API Key
parameter: token
value: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjQ3IiwidW5pcXVlX3VzZXJfaWQiOiIxMTg4IiwibmFtZSI6IkJhcm1lciBTb3V0aCBDb29yZGluYXRvciIsInVzZXJuYW1lIjoiMjA0MCIsIm1vYmlsZSI6Ijk5OTk5OTk5OTkiLCJsZXZlbF9pZCI6IjUiLCJwYXNzd29yZF9yZXNldF90b2tlbiI6bnVsbCwiZW1haWwiOiJhYmNuZ0BnbWFpbC5jb20iLCJhdXRoX2tleSI6ImQ0MWQ4Y2Q5OGYwMGIyMDRlOTgwMDk5OGVjZjg0MjdlIiwic3RhdHVzIjoiMSIsImNyZWF0ZWRfYXQiOiIwMDAwLTAwLTAwIDAwOjAwOjAwIiwidXBkYXRlZF9hdCI6IjIwMTktMTEtMDYgMDc6MTg6MDQiLCJjaGFuZ2VkX3Bhc3N3b3JkX29uY2UiOm51bGwsInVzZXJfZXJyb3JfZnJlcXVlbmN5XzEiOm51bGwsInVzZXJfZXJyb3JfZnJlcXVlbmN5XzIiOiIxOCIsInVzZXJfbGV2ZWwiOiJDbHVzdGVyIFN1cGVydmlzb3IiLCJsb2dpbl9pZCI6M30.ak3OSYopsW211SvJnzH4nNLvcj9Z4B16fR329fZbHeA
This problem persists when I try doing this with PHP CURL too using "PUT" or "POST" method.
PHP CURL Code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "district_id=1");
curl_setopt($curl, CURLOPT_URL, "https://hrtaskapi.dhwaniris.in/index.php/api/beneficiary/child-profile-create");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEwMTEiLCJ1bmlxdWVfdXNlcl9pZCI6IjEwMTEiLCJuYW1lIjoiU3dhdGkgU29tYW5pIiwidXNlcm5hbWUiOiIxNDEyIiwibW9iaWxlIjoiNjM3NzE0NzU3MiIsImxldmVsX2lkIjoiNSIsInBhc3N3b3JkX3Jlc2V0X3Rva2VuIjoiMTQyNDg3IiwiZW1haWwiOiJzd2F0aXNvbWFuaTk1QGdtYWlsLmNvbSIsImF1dGhfa2V5IjoiZDQxZDhjZDk4ZjAwYjIwNGU5ODAwOTk4ZWNmODQyN2UiLCJzdGF0dXMiOiIxIiwiY3JlYXRlZF9hdCI6IjAwMDAtMDAtMDAgMDA6MDA6MDAiLCJ1cGRhdGVkX2F0IjoiMjAxOS0xMC0xNSAwNzowNjoxMiIsImNoYW5nZWRfcGFzc3dvcmRfb25jZSI6bnVsbCwidXNlcl9lcnJvcl9mcmVxdWVuY3lfMSI6bnVsbCwidXNlcl9lcnJvcl9mcmVxdWVuY3lfMiI6IjMiLCJ1c2VyX2xldmVsIjoiQ2x1c3RlciBTdXBlcnZpc29yIiwibG9naW5faWQiOjR9.jDfocuaZhPcTyefEm-ZH-VOdPEsUdc-r8-bdAW-MJwI',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
curl_close($curl);
echo "<pre>";
print_r(json_decode($result));
CURL response:
stdClass Object
(
[success] =>
[status] => 200
[message] => district Id is required
)
When I call this API Link https://hrtaskapi.dhwaniris.in/index.php/api/beneficiary/get-child-profile using CURL in PHP I get the following response:
stdClass Object
(
[success] => 1
[status] => 200
[message] => Child Profile Detail
[timestamp] => 1614518328
[child_profile] => Array
(
[0] => stdClass Object
(
[id] => 51
[name] => Laxmi
[sex] => 2
[dob] => 2014-04-02
[father_name] => Kishansingh
[mother_name] => Uddi
[district_id] => 1
[photo] => https://staging-dhwani.s3.ap-south-1.amazonaws.com/82ac44b1c5d7ef91a9e912abca5fc3f5.jpg
)
[1] => stdClass Object
(
[id] => 52
[name] => Laxmi
[sex] => 2
[dob] => 2012-11-07
[father_name] => Vinod nath
[mother_name] => Babli bai
[district_id] => 1
[photo] => https://staging-dhwani.s3.ap-south-1.amazonaws.com/82ac44b1c5d7ef91a9e912abca5fc3f5.jpg
)
)
)
Above is the district_id that is required. If it will accept the district id then a new record will be created.
I am trying to fetch videos from youtube using below code:
$youtube_api_url = "https://www.googleapis.com/youtube/v3/search?key=".$new_key->api_key;
$youtube_api_url = $youtube_api_url."&order=relevance&type=video&maxResults=20&part=snippet&videoCategoryId=10&q=". urlencode(preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '',$trackRecord->name));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$youtube_api_url);
$result=curl_exec($ch);
$response = json_decode($result, true);
echo "<pre>"; print_r($response); die;
It is returning wrong number of results as below:
Array
(
[items] => Array
(
[0] => Array
(
[kind] => youtube#searchResult
[etag] => "m2yskBQFythfE4irbTIeOgYYfBU/ne5AZXpdoLtAn-mlwu4ipioOQ_I"
[id] => Array
(
[kind] => youtube#video
[videoId] => dB0ITzfbwfw
)
[snippet] => Array
(
[publishedAt] => 2017-01-23T17:31:23.000Z
[channelId] => UCdW5nPo1oyD2vLkvoYijmEg
[title] => Lihtz Kamraz "The Switch Up" Interview Part 2
[description] => In 2016, a lot of break out artists emerged and made a mark in the game. As 2017 begins, one name that you should definitely be on the lookout for is Lihtz ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/dB0ITzfbwfw/default.jpg
[width] => 120
[height] => 90
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/dB0ITzfbwfw/mqdefault.jpg
[width] => 320
[height] => 180
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/dB0ITzfbwfw/hqdefault.jpg
[width] => 480
[height] => 360
)
)
[channelTitle] => HIPHOPSINCE1987TV
[liveBroadcastContent] => none
)
)
)
)
As you can see it is showing total and per page result as:
[pageInfo] => Array
(
[totalResults] => 14
[resultsPerPage] => 20
)
How can I get all results?
What I am trying to do is to get the value of the first element, [0]['uri'] from my result curl api. I have an associative array from the curl_exec but can't figure out how to extract the data to find the value of the first element. I have tried echo $alerts[0]['uri'] but it doesn't work.
<?php
// Initiate curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type: application/json',"Authorization: Bearer $access_token"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
$alerts = json_decode($result,true);
print_r($alerts);
//echo $alerts[0]['uri'] doesn't work.
?>
Output Result:
Array (
[result] => success
[message] => Array (
[alerts] => Array (
[0] => Array (
[id] => 78591963
[uri] => htps://access.rrr.com/interface/open_api/api/alerts/78591963
)
[1] => Array (
[id] => 78576283
[uri] => htps://access.rrr.com/interface/open_api/api/alerts/78576283
)
[2] => Array (
[id] => 78576209
[uri] => htps://access.rrr.com/interface/open_api/api/alerts/78576209
)
[3] => Array (
[id] => 78572644
[uri] => htps://access.rrr.com/interface/open_api/api/alerts/78572644
)
[4] => Array
)
)
)
)
Answer was given by other gentlemen in the comment.
I failed to look at the array structure. It is simply:
$alerts['message']['alerts'][0]['uri']
I am using php curl to get account and transaction data from the plaid api. I'd like to see it in a nice format so I can go about creating loops to save the information into a database. The decoded json output, when printed with print_r, is returned like this:
array ( [accounts] => Array ( [0] => Array ( [_id] => QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK [_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA [_user] => eJXpMzpR65FP4RYno6rzuA7OZjd9n3Hna0RYa [balance] => Array ( [available] => 1203.42 [current] => 1274.93 ) [institution_type] => fake_institution [meta] => Array ( [name] => Plaid Savings [number] => 9606 ) [subtype] => savings [type] => depository ) [1] => Array ( [_id] => nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy [_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA...
I'd like to get it looking like this:
Array (
[accounts] => Array (
[0] => Array (
[_id] => QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK
[_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA
[_user] => eJXpMzpR65FP4RYno6rzuA7OZjd9n3Hna0RYa
[balance] => Array (
[available] => 1203.42
[current] => 1274.93 )
[institution_type] => fake_institution
[meta] => Array (
[name] => Plaid Savings
[number] => 9606 )
[subtype] => savings
[type] => depository )
[1] => Array (
[_id] => nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy
[_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA
`
For reference, here is the code to get the response:
$data = array(
"client_id"=>"test_id",
"secret"=>"test_secret",
"access_token"=>"test_chase"
);
$string = http_build_query($data);
//initialize session
$ch=curl_init("https://tartan.plaid.com/connect/get");
//set options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute session
$accountData = json_decode(curl_exec($ch), true);
print_r($accountData);
//close session
curl_close($ch);
Any thoughts other than loops?
I'm running a curl request and it returns the below:
Array
(
[#attributes] => Array
(
[exp_cid] => 0BqO
)
[REQUEST] => Array
(
[#attributes] => Array
(
[type] => RETURN
[subtype] => CALLBUR
[EXP_ExperianRef] =>
[success] => Y
[timestamp] => Mon, 10 Feb 2014 at 10:28 AM
[id] => 0BqO
)
[MB01] => Array
(
[#attributes] => Array
(
[seq] => 01
)
[DATEOFTRANSACTION] => 20100901
[VRM] => MYREG
[VINCONFIRMATIONFLAG] => 0
[ENGINECAPACITY] => 01360
[DOORPLAN] => 14
[DATEFIRSTREGISTERED] => 20041007
[YEAROFMANUFACTURE] => 2004
[SCRAPPED] => 0
[EXPORTED] => 0
[IMPORTED] => 0
[MAKE] => PEUGEOT
[MODEL] => 307 ENVY 90
[COLOUR] => BLACK
[TRANSMISSION] => MANUAL 5 GEARS
[ENGINENUMBER] => EE
[VINSERIALNUMBER] => VF
[DOORPLANLITERAL] => 5 DOOR HATCHBACK
[MVRISMAKECODE] => L1
[MVRISMODELCODE] => BJJ
[DTPMAKECODE] => L1
[DTPMODELCODE] => 892
[TRANSMISSIONCODE] => M
[GEARS] => 5
[FUEL] => PETROL
[CO2EMISSIONS] => 155
[USEDBEFORE1STREG] => 0
[IMPORTNONEU] => 0
[UKDATEFIRSTREGISTERED] => 20041007
[MAXPERMISSIBLEMASS] => 01639
[MAXNETPOWER] => 085
[MAXTRAILERWEIGHTBRAKED] => 01000
[MAXTRAILERWEIGHTUNBRAKED] => 00620
[SOUNDLVLSTATIONARY] => 079
[SOUNDLEVELENGINESPEED] => 03938
[SOUNDLVLDRIVEBY] => 71
[SEATINGCAPACITY] => 005
[MASSINSERVICE] => 000001245
[MAKEMODEL] => PEUGEOT 307 ENVY 90
)
[MB37] => Array
(
[#attributes] => Array
(
[seq] => 01
)
[V5CDATACOUNT] => 02
[V5CDATAITEMS] => Array
(
[0] => Array
(
[DATE] => 20090825
)
[1] => Array
(
[DATE] => 20041007
)
)
)
)
)
I'm struggiling to access the return, I've tried the following but it doesn't return anything:
foreach ($xml->REQUEST->MB01 as $requestData) {
echo $requestData;
}
My other problem is depending on the vehicle reg it will return different MB blocks, is there away to return them if they are returned.
Thanks.
EDIT
This is the full code that I'm running, when it's ran all I get is < Does this mean anything?
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output[0]['REQUEST']['MB01']['DATEOFTRANSACTION'];
Cheers guys.
The return is an array, not an object. Try this :
foreach ($xml['REQUEST']['MB01'] as $requestData) {
if(is_array($requestData) {
print_r($requestData);
} else {
echo $requestData;
}
}