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
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
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
I don't understand. Do you have two arrays?
What array do you have and what data do you want to obtain?
mlucicom
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";
}
?>
Similar Topics
-
Get foreach-result separated
PHP - MySQL
First post
Plessant Coursesweb
I`m still busy to read out colors my first tryout did not work.
In foreach code below I getting 20 results in once,
But I...
Last post
Marplo, I coming back some later time on it.
Because I need to re-study the script.... Also, because the data from $colors (excuse).
-
Get the length of a JSON object in JS
JavaScript - jQuery - Ajax
First post
I have converted a database table into a JSON object.
This is in essence a two-dimensional array, with each record being the first dimension, which...
Last post
You can use Object.keys to get the list of keys in the Object as an array and then get the length from it:
Object.keys(act_obj_array).length;
-
How to escape double quote in JSON string
JavaScript - jQuery - Ajax
First post
I am trying to parse a JSON string 'Hello test' containing double quote which already escaped.
JSON.parse('{ x : Hello \ test }')
But I get...
Last post
You just need to escape the backslash \, so it turns into two backslashes \\
let obj = JSON.parse('{ x : Hello \\ test }')
console.log(obj)
-
Ajax-PHP Rating Stars-SyntaxError JSON.parse unexpected character
Scripts from this website
First post
The Ajax-PHP Rating Stars Script , I am trying to get it working but I keep getting this error:
SyntaxError: JSON.parse: unexpected character at...
Last post
Awesome, Finally I get it works, after adding this line alert(obajx.responseText); it guided me to what was missing, first of all, the files URL's...