How to parse this multi-dimensional array properly - php

I have tried everything I know and I have failed totally to parse this multi dimensional array so that I can get the "symbol" keys and corresponding values of "contractType" , but I can't seem to get it.
The array is generated here: https://fapi.binance.com/fapi/v1/exchangeInfo
So I am doing the following:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
$results = [];
foreach ($data['symbols'] as $info) {
$results[] = $info['symbol'];
}
print_r($results);
I tried a foreach loop, for loop, tried various offsets eg. $data[0]['symbols']..
to $data[9]['symbols'] etc. but can't seem to get the correct array offset.
10000"}],"symbols":[{"symbol":"BTCUSDT","pair":"BTCUSDT","contractType":"PERPETUAL","deliveryDate
I'm trying to loop through the "symbols" offset within this main array, and get 1. the "symbol" 2. its corresponding "contractType"
Ty..

It doesn't work because the CURL result is in JSON.
You got to convert it to an array before you can loop through it.
$data = json_decode($data, true);

It looks like you need to convert the JSON response into an array before you loop over it.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
$results = [];
$data = json_decode($data);
$i = 0;
foreach ($data->symbols as $info) {
$results[$i]['symbol'] = $info->symbol;
$results[$i]['contractType'] = $info->contractType;
$i++;
}
print_r($results);

Yes. Thankyou.. (didn't understand answer properly.)
$url = 'https://fapi.binance.com/fapi/v1/exchangeInfo';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
$data = json_decode($data, true);
$results = [];
$symbols = $data['symbols'];
$i = 0;
foreach ($symbols as $info) {
$results[$i]['symbol'] = $info['symbol'];
$results[$i]['contractType'] = $info['contractType'];
$i++;
}
print_r($results);
Much appreciated, ty..

Related

How to use cURL in for loop?

My for loop can run twice, but it only run once when I use the cURL in for loop
My for loop can run twice, but it only run once when I use the cURL in for loop
public function index_onSync () {
$checkedIds = post('checked');
$data = ModelsAdmin::select('id', 'address', 'private_key')->whereIn('id', $checkedIds)->get();
$sync_data = array();
for ($i=0; $i < count($data); $i++) {
$url_confirm = "http://127.0.0.1:50233/sync";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_confirm);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
$post_data = array( "id"=>$data[$i]['id'], "address"=>$data[$i]['address'], "private_key"=>$data[$i]['private_key'] );
$json_data = json_encode($post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '.strlen($json_data)
));
curl_setopt($ch, CURLOPT_POSTFIELDS,$json_data);
$data = curl_exec($ch);
$data = json_decode($data);
array_push($sync_data, $data);
echo $data;
curl_close($ch);
}
var_dump($sync_data);
}
You're overriding your $data array from ModelsAdmin::select in line
$data = curl_exec($ch);
So, on the second iteration, your data is not a 2-rows array anymore...
Instead of $data, you could use $response, for example...

Unable to pass an ARRAY through CURL?

$items[] = array("sku"=>"data","name"=>"data","amount"=>0,"qty"=>"0","id"=>"data","price"=>0,"url"=>"data");
$post = array(
'data' => 'data',
'items' => $items);
$ch = curl_init('urltopost');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 3); //timeout in seconds
header('Content-Type: text/html');
echo curl_exec($ch);
// Check if any error occurred
if(curl_errno($ch))
{
header('Location: error');
die();
}
If I post it to itself and do var_dump($_POST["items"]); I just get string(5) "Array" as the output.
I also attempted a foreach loop which outputted no data.
Am I being stupid and something glaringly wrong is up with it?
You can try this
$post = [];
foreach($items as $index=>$item){
$new_key = "item[$index]";
foreach($item as $k=>$v){
$post[$new_key.'['. $k .']'] = $v;
}
}
$post['data'] = 'data';
The post array format is array('key'=>string, ....), it not allow array. If you want post array, the key format is 'key[index1][index2] ...'. In server you will receive $_POST['key'] as array.

How i can get input value with php curl?

İ want login instagram with php curl. When I try to login below code is always changing So i must get this input value:
<input type="hidden" name="csrfmiddlewaretoken" value="uCbmYHcQLe18VAvVFXbfyPcS5kXtCuuE">
my curl code is:
<?php
$url = "https://www.instagram.com/accounts/login/?force_classic_login";
$values = "csrfmiddlewaretoken=" + $key + "&username=myusername&password=mypassword";
$key = "";
//foreach input name csrfmiddlewaretoken = $key
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $values);
$veri = curl_exec($curl);
curl_close($curl);
echo $veri;
?>
Try this:
$veri = curl_exec($curl); // That $veri contains the curl response into it
$data = json_decode($veri, true); // Convert the response into an array
now the $data is an array and you can access the values from this array by using its indexes.
Man, explode the response content with the separator <input then get on array result your data.
Do something like this:
$result = explode('<input type="hidden" name="csrfmiddlewaretoken" value="', $file_content);
$result = explode('">', $result[1] );
$input_value = $result[0];
$input_value = $result[0];
i hope this help

json decode into array

I'm trying to store the titles in an array and extract the long cmcontinue string from the below url.
http://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:1980_births&format=json
my current code:
$url = 'http://en.wikipedia.org/w/api.php?
action=query&list=categorymembers&cmtitle=Category:'.$cat.'&format=json';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "asdf");
$c = curl_exec($ch);
$json = json_decode($c);
$array = $json->{'query'}->{'categorymembers'}->{'title'};
try adding second paremeter of json_decode, like:
$json = json_decode($c, true);
And get cmcontinue value as:
echo $json["query-continue"]["categorymembers"]["cmcontinue"];
For titles:
$titles = array();
foreach($json["query"]["categorymembers"] as $vals) {
array_push($titles, $vals["title"]);
}
echo "<pre>"; print_r($titles);

php youtube json decode

I tried to use JSON decode to get the youtube API feed. However, when I paste the JSON result in http://www.jsonlint.com/ I noticed something like
"media$group": {
"media$category": [
Unfortunately some symbols are rejected by php. Here is my code, I tried to remove this $ symbol, but maybe not success. How do I solve this?
$url = 'http://gdata.youtube.com/feeds/api/videosq=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
...
}
Your problem is the usage of PHP identifiers to access the contents. The simplest solution here would be to get an array instead of an object:
$data = json_decode ( $json , $assoc = true );
This allows access to fields with:
echo $result['media$group']['media$description'];
If you want to keep the object syntax, that's possible with this kludge:
echo $result->{'media$group'}->{'media$category'};
(But arrays are safer here. You don't get a fatal error should the format change and properties be absent.)
This work:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos?q=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
var_dump($result);
}
?>

Categories