Foreach JSON RESPONSE from Api
Discuss coding issues, and scripts related to PHP and MySQL.
-
mlucicom
- Posts:37
Foreach JSON RESPONSE from Api
Hello, i have a JSON response from an api that is like this:
mluci.com/script.php
I tried to transform this to an array but i can't foreach this.For every campaign i need immpressions, conversion_profit and cost to a database.
Code: Select all
<?php
//The url you wish to send the POST request
$url= 'popads.net/api/report_advertiser?key=9a790b65025e0aa449e60384a87b56e97c3ebf39&groups=campaign';
//The data you want to send via POST
$fields = [
'__VIEWSTATE ' => $state,
'__EVENTVALIDATION' => $valid,
'btnSubmit' => 'Submit'
];
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
echo $url;
$result = curl_exec($ch);
//$array = json_encode($result);
echo $result;
?>
Admin
Posts:805
Hello,
1. The $state and $valid variable are not defined; so, you must define them with the values you know.
2. To convert a JSON string to array, use json_decode().
Try the following code, see the resulted array,then build your code in foreach to get the values you want and to add them in your database
Code: Select all
<?php
//Here define the variables
$state ='';
$valid ='';
//The url you wish to send the POST request
$url= 'https://www.popads.net/api/report_advertiser?key=9a790b65025e0aa449e60384a87b56e97c3ebf39&groups=campaign';
//The data you want to send via POST
$fields = [
'__VIEWSTATE ' => $state,
'__EVENTVALIDATION' => $valid,
'btnSubmit' => 'Submit'
];
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
$arr = json_decode($result, true);
//Here build your code to get the data from $arr
foreach($arr as $k=>$v){
//etc.
}
//Test to see array structure
echo '<pre>';
var_export($arr);
mlucicom
Posts:37
I resolved this, but have another question.
if i have an array with values from database '.$database['campaign'].'
How can i get from array "rows" only values that have rows['name'] in database:
Code: Select all
foreach($arr['rows'] as $row){
echo $row['campaign'];
}
Admin
Posts:805
I don't understand. Do you have two arrays?
What array do you have and what data do you want to obtain?
mlucicom
Posts:37
I resolved with this thank you:
I changed the code but this send only an api request :
Code: Select all
<?php
$servername = "localhost";
$username = "mluci_api";
$password = "lume010199&&";
$dbname = "mluci_api";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM api";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$state ='';
$valid ='';
//The url you wish to send the POST request
$url= 'popads.net/api/report_advertiser?key=9a790b65025e0aa449e60384a87b56e97c3ebf39&campaigns='.$row['id_campaign'].'&groups=website';
//The data you want to send via POST
$fields = [
'__VIEWSTATE ' => $state,
'__EVENTVALIDATION' => $valid,
'btnSubmit' => 'Submit'
];
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
$arr = json_decode($result, true);
//Here build your code to get the data from $arr
foreach($arr as $k=>$v){
//etc.
}
//Test to see array structure
echo '<pre>';
var_export($arr);
}
} else {
echo "0 results";
}
?>