Related
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..
İ 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
I Have this data from my API Call.
[{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"},{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Book+-+David+%26+Goliath+%3B+Face+Cream+-+Clinique%2FGlycolix\",\"trackingnumber\":\"9.36E+21\",\"packageweight\":\"2.0000\",\"weightunit\":\"Lbs\",\"price\":\"18.0000\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Sunglasses+-+Valentino\",\"trackingnumber\":\"1.02E+33\",\"packageweight\":\"0.5000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Sunglasses+-+Safilo+group\",\"trackingnumber\":\"1.01E+33\",\"packageweight\":\"0.8000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Pigmentation+Color+Tobacco\",\"trackingnumber\":\"'42060106''9405510200830072094975'\",\"packageweight\":\"0.6300\",\"weightunit\":\"Lbs\",\"price\":\"320.0000\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"}]
How can I convert this data into PHP Array? I tried using json_decode($result,true) but it is not working properly. Thanks in advance.
UPDATE:
My PHP Code
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode(stripslashes($result), true);
$json=str_replace("\\",'', $result);
$jsondata=json_decode($json,true);
print_r($jsondata);
//echo $result;
use json_decode() for this:
// It will convert the given json to an array
$arr = json_decode($json, true);
// The second param is for array, o/w it will return an object
Reference
$json='[{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"},{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Book+-+David+%26+Goliath+%3B+Face+Cream+-+Clinique%2FGlycolix\",\"trackingnumber\":\"9.36E+21\",\"packageweight\":\"2.0000\",\"weightunit\":\"Lbs\",\"price\":\"18.0000\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Sunglasses+-+Valentino\",\"trackingnumber\":\"1.02E+33\",\"packageweight\":\"0.5000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Sunglasses+-+Safilo+group\",\"trackingnumber\":\"1.01E+33\",\"packageweight\":\"0.8000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Pigmentation+Color+Tobacco\",\"trackingnumber\":\"420601069405510200830072094975\",\"packageweight\":\"0.6300\",\"weightunit\":\"Lbs\",\"price\":\"320.0000\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"}]';
$json=str_replace("\\",'', $json);
$jsondata=json_decode($json,true);
print_r($jsondata);
CURL output is json and in line $json_result = json_decode($result, true); you decoded it to an array, after str_replace you have an array but you decoded it again, may be if you remove $jsondata=json_decode($json,true); and return $json you receive an array
Your string \"trackingnumber\":\"'42060106''9405510200830072094975'\" has syntax error.
$result = '[{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"},{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Book+-+David+%26+Goliath+%3B+Face+Cream+-+Clinique%2FGlycolix\",\"trackingnumber\":\"9.36E+21\",\"packageweight\":\"2.0000\",\"weightunit\":\"Lbs\",\"price\":\"18.0000\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Sunglasses+-+Valentino\",\"trackingnumber\":\"1.02E+33\",\"packageweight\":\"0.5000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Sunglasses+-+Safilo+group\",\"trackingnumber\":\"1.01E+33\",\"packageweight\":\"0.8000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Pigmentation+Color+Tobacco\",\"trackingnumber\":\"420601069405510200830072094975\",\"packageweight\":\"0.6300\",\"weightunit\":\"Lbs\",\"price\":\"320.0000\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"}]';
$json_result = json_decode(stripslashes($result), true);
$json=str_replace("\\",'', $result);
$jsondata=json_decode($json,true);
print_r($jsondata);
Try this,
$jsonData = stripslashes(html_entity_decode($result));
$output = json_decode($jsonData, true);
echo "<pre>";
print_r($output);
echo "</pre>";
Or Try this way,
$out = preg_replace('/\\\"/',"\"", $result);
$output = json_decode($out, true);
echo "<pre>";
print_r($output);
echo "</pre>";
I'm calling a function to get data from 3 different sources.
The $returnedData response is always an array. How to merge all 3 responses into one array?
function getData($xPostURL,$xToken,$xTokenSecret,$xAccount)
{
$datatopost = array (
"token" => $xToken,
"tokenSecret" => $xTokenSecret,
"account" => $xAccount,
);
$ch = curl_init ($xPostURL);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returnedData = curl_exec ($ch);
echo $returnedData;
}
getData("http://www.example.com/foo.php","","","");
getData("http://www.example.org/bar.php","","","");
getData("http://www.example.net/helloworld.php","","","");
Try use: array_merge
$a = getData("http://www.example.com/foo.php","","","");
$b = getData("http://www.example.org/bar.php","","","");
$c = getData("http://www.example.net/helloworld.php","","","");
$r = array_merge($a,$b,$c);
You can merge Arrays with this function http://php.net/manual/en/function.array-merge.php
If wel use this function your code will look something like this:
function getData($xPostURL,$xToken,$xTokenSecret,$xAccount)
{
$datatopost = array (
"token" => $xToken,
"tokenSecret" => $xTokenSecret,
"account" => $xAccount,
);
$ch = curl_init ($xPostURL);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returnedData = curl_exec ($ch);
return $returnedData;
}
$firstData = getData("http://www.example.com/foo.php","","","");
$secondData = getData("http://www.example.org/bar.php","","","");
$thirdData = getData("http://www.example.net/helloworld.php","","","");
$merged = array_merge($firstData, $secondData, $thirdData);
Now everything is in $merged
(Also did some indenting and changed echo $returnedData; to return $returnedData; echo only displays it, return returns it to the variable.
im new to php, I want to get many data and output them from different urls, so actually I made one that works and gets one data.. so far I have searched and looked so many.. but I couldn't figure how to get multiples ones..
so one thing to consider : that yellow text I have highlighted has more than 100.. i mean eid=1 to eid=102.. each one has data stored in it.
my php code is this
<?php
$eType=2;
$eId=43;
$lType=1;
$dNames=drivername;
$shard="Apex";
$session = curl_init();
curl_setopt ($session, CURLOPT_URL, "http://world.needforspeed.com/SpeedAPI/ws/game/nfsw/leaderboards?output=xml");
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, "et=".$eType."&eid=".$eId."<=".$lType."&dn=".$dNames."&shard=".$shard);
curl_setopt ($session, CURLOPT_HEADER, true);
curl_setopt ($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
// Get the XML from the response, bypassing the header
if (!($xml = strstr($response, '<?xml'))) {
$xml = null;
}
// Output the XML
$worldLeaderboard = simplexml_load_string($xml);
foreach ($worldLeaderboard->worldLeaderboard as $world){
$rank = $world['rank'];
$name = $world['personaName'];
$car = $world['make'];
$model = $world['carName'];
$duration = $world['eventDuration'];
echo <<<EOD
$rank $name $car $model $duration
EOD;
}
?>
as you can see my code, it works fine and outputs from that data.. I know I can do multiple by coping all the code and pasting it 100 times, and changing each $eid variable value but that's too much and data consuming I assume..
Make a function for the curl calls, put the URLs in an array and loop through the array and use each entry in the array on the curl function you wrote.
function my_curl_function($url)
{
// curl call
return $result;
}
$urls[] = 'url1';
$urls[] = 'url2';
$urls[] = 'url3';
// etc
foreach($urls AS $url)
{
echo my_curl_function($url);
}
You could use a simple 'for' loop.
for($eId=1;$eId<=102;$eId++) {
$eType=2;
$lType=1;
$dNames=drivername;
$shard="Apex";
$session = curl_init();
curl_setopt ($session, CURLOPT_URL, "http://world.needforspeed.com/SpeedAPI/ws/game/nfsw/leaderboards?output=xml");
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, "et=".$eType."&eid=".$eId."<=".$lType."&dn=".$dNames."&shard=".$shard);
curl_setopt ($session, CURLOPT_HEADER, true);
curl_setopt ($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
// Get the XML from the response, bypassing the header
if (!($xml = strstr($response, '<?xml'))) {
$xml = null;
}
// Output the XML
$worldLeaderboard = simplexml_load_string($xml);
foreach ($worldLeaderboard->worldLeaderboard as $world){
$rank = $world['rank'];
$name = $world['personaName'];
$car = $world['make'];
$model = $world['carName'];
$duration = $world['eventDuration'];
echo <<<EOD
$rank $name $car $model $duration
EOD;
}
}