I try to get values of stdClass Object into php variables. And i looked for other solutions but i couldn't success. Here is my result.
stdClass Object
(
[messages] => Array
(
[0] => stdClass Object
(
[apiMessageId] => db55468cdb274551b9e18c973ec780f4
[accepted] => 1
[to] => 1111111
[error] =>
)
)
[error] =>
)
I want to get only "apiMessageId" section but as i said i couldn't get this. I tried "$object->messages[0]->apiMessageId;" but no results on the screen. Anyone to help me please?
and this is my php code for request.
function Conn($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
$cikti = curl_exec($curl);
curl_close($curl);
return str_replace(array("\n","\t","\r"), null, $cikti);
}
$url1 = "https://platform.clickatell.com/messages/http/send?apiKey=xxx&to=xxx&content=Test+message+text";
$Conn = Conn($url1);
$res = json_decode($Conn);
echo "<pre>";
print_r($res);
echo "</pre>";
I found the solution. This is the query what i need.
echo $res->messages[0]->apiMessageId;
I wrote before
echo $object->messages[0]->apiMessageId;
but my variable name is not object. The variable name is $res.
Related
I need to grab XML data from an .asp URL and cannot figure out where it goes wrong. Tried:
<?php
$url = "http://bookings.emperordivers.com/webScheduleSpecificXML_all.asp";
$feed = file_get_contents($url);
$xml = simplexml_load_string($feed);
// Display the first post title
echo $xml->Schedules->Schedule[0]->Boat;
I hoped this would work, but suspected that the .asp URL is blocking somehow so alternatively tried:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('http://bookings.emperordivers.com/webScheduleSpecificXML_all.asp');
$xml = simplexml_load_string($returned_content);
// Display the first post title
echo $xml->Schedules->Schedule[0]->Boat;
In both case a beautiful white screen. Is there a simple trick to get this working?
Your not accessing it correctly:
echo $xml->Schedule[0]->Boat;
The format is:
SimpleXMLElement Object
(
[Schedule] => Array
(
[0] => SimpleXMLElement Object
(
[Start] => 2017-09-24
[Duration] => 7
[Boat] => MV Emperor Orion
[Itinerary] => Best of Maldives
[Dep-Arr] => Male
[Spaces] => 4
[Rates] => SimpleXMLElement Object
(
[Rate] => Array
(
[0] => 1149
[1] => 1,057.08
[2] => 1,367.31
)
)
)
...
I need one help.I need to get the json decode value inside foreach loop using PHP. I am explaining my code below.
$user_qry = "http://oditek.in/takeme/webservice/user2/user_ride_list_v2.php";
$pstflds="mobile=".$mobile;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $user_qry);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pstflds);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response=curl_exec ($ch);
$data=json_decode($response,true);
$bikeArr=$data->data;
print_r($bikeArr);exit;
Its giving the output like below.
Array ( [0] => stdClass Object ( [book_id] => 161117193881 [booking_status] => 4 [book_date] => 17-11-2016 [book_time] => 07:38 PM ) [1] => stdClass Object ( [book_id] => 161116183564 [booking_status] => 4 [book_date] => 16-11-2016 [book_time] => 06:35 PM ) )
I need to iterate the above array in foreach loop and get the data.Please help me.
json_decode parameter 2 is assoc
When TRUE, returned objects will be converted into associative arrays.
default is false
reference http://php.net/manual/en/function.json-decode.php
json_decode($response, true);
try
$data=json_decode($response,true);
and youll get assoc array instead of object
Can you try this and see if you get any errors?
foreach($bokeArr as $bike){
echo 'book id = ' . $bike->book_id;
}
I have an array like this:
Array ( [10209064245580796] => Array ( [0] => Array ( [hashed_token] => ) [1] => Array ( [password] => ) [2] => Array ( [email] => klemen.pevc#gmail.com ) ) [10207252567926988] => Array ( [0] => Array ( [hashed_token] => ) [1] => Array ( [password] => 716b64c0f6bad9ac405aab3f00958dd1 ) [2] => Array ( [email] => milvuk#gmail.com ) ) )
That I made this way:
$users looks like this:
Array ( [0] => 10209064245580796 [1] => 10207252567926988)
$arrayOfFields looks like this:
Array ( [0] => hashed_token [1] => password [2] => email )
So:
$array=array();
foreach($users as $user){
$array[$user]=array();
foreach($arrayOfFields as $getFieldValue) {
$user = '' . $user . '';
$query = "SELECT `$getFieldValue` FROM $table WHERE `$column`= $user";
$result = $mysqli->query($query);
$fetchResult = $result->fetch_assoc();
$getFieldValue = '' . $getFieldValue . '';
$finalValue = $fetchResult[$getFieldValue];
array_push($array[$user] ,array($getFieldValue=>$finalValue));
}
}
And after this two foreachs I get an array $array as shown above in first example that I'm sending via cURL with this code:
$data = array('facebook_app_id' => $facebook_app_id, 'facebook_ids' => $facebook_ids,'values_for_custom_fields' => $array);
$endpoint_url = 'https://servis-racunara.net/api/index.php';
$curl = curl_init($endpoint_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Authorization: Token ".$token));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$curl_response = curl_exec($curl);
curl_close($curl);
//you do not need to print results, this is just for debugging purposes
$result = $curl_response;
print_r($result);
So inside script of this endpoint https://servis-racunara.net/api/index.php (on which I'm sending some data, including $array) when i do print_r($_POST) I get an array like this:
Array ( [facebook_app_id] => 1512823699024883 [facebook_ids] => 10209064245580796,10207252567926988[values_for_custom_fields] => Array )
So, under key values_for_custom_fields is stored $array array that I need to process. When I do print_r($_POST['values_for_custom_fields']); it just says 'Array', and when I try this:
foreach($_POST['values_for_custom_fields'] as $anything) {
echo($anything);//same with print_r
}
Server says :
Warning: Invalid argument supplied for foreach()
Any ideas?
The problem is that you've chosen wrong format for sending data. In your code you use form submission format, namely application/x-www-form-urlencoded (as it is used by default in CURL). Neither of form submission formats support nested data (arrays). In other words you cannot send nested arrays in POST request as form-encoded data.
So you have to use other format to send your data. I would suggest to use JSON - popular format which allow any level of nesting. In this case you have to JSON-encode data before sending and decode - when receiving them in the endpoint script. Also you have to set appropriate Content-type header for request: application/json.
...
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Authorization: Token ".$token,
"Content-type: application/json"
));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
...
I have the following array of json data. When I try to loop using a for each, php gives me the error "Object of class stdClass could not be converted to string". I want to access the nameserver in the authns array, and the soa array. How do I go about doing that?
stdClass Object (
[error] =>
[domain] => whoisdoma.net
[ip_address] => 108.162.199.120
[authns] => stdClass Object (
[nameserver] => uma.ns.cloudflare.com
[ip] => 173.245.58.146
[location] => San Francisco
)
[soa] => stdClass Object (
[nameserver] => tom.ns.cloudflare.com
[email] => dns.cloudflare.com
[serial] => 2015505396
[refresh] => 10000
[retry] => 2400
[expiry] => 604800
[minimum] => 3600
)
)
This is the code I'm using to get the data:
<?php
$domain = $_GET['domain'];
//set the url
$url = "http://api.site.ga/v1/dns/lookup?domain=".$domain;
//start a curl session
$ch = curl_init();
$timeout = 5;
//set curl options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//get the data
$data = curl_exec($ch);
//decode the data
$jsonData = json_decode($data);
//close the curl session
curl_close($ch);
?>
This is the for each loop I'm using
<?php
foreach($jsonData->authns as $authns)
{
echo $authns->nameserver;
}
?>
You need to decode the json object using an associative array.
Here is an exmaple:
$data = json_decode(file_get_contents('http://api.domain.com/call'), true);
$data is now an associative array.
You are looping through authns while it is an object by itself. So your nameserver which you want is actually $authns in its first iteration.
Just access it like $jsonData->authns->nameserver
try this for yourself
foreach($jsonData->authns as $authns)
{
echo $authns;
}
I am sending data from my local machine to server using CURL. And the data is multidimensional array.
Array
(
[0] => stdClass Object
(
[id] => 1
)
[1] => stdClass Object
(
[id] => 0
)
[2] => stdClass Object
(
[id] => 11
)
)
I am using this below code for sending the data.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "my_url");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array); // $array is my above data
But at server when I try to put this incoming data to file or just print_r it gives me this below output
Array
(
[0] => Array
[1] => Array
[2] => Array
)
But I want the output in multidimensional.
I tried with print_r($_POST[0]) but it gives only Array text.
cURL can only accept a simple key-value paired array where the values are strings, it can't take an array like yours which is an array of objects. However it does accept a ready made string of POST data, so you can build the string yourself and pass that instead:
$str = http_build_query($array);
...
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
A print_r($_POST) on the receiving end will show:
Array
(
[0] => Array
(
[id] => 1
)
[1] => Array
(
[id] => 0
)
[2] => Array
(
[id] => 11
)
)
I would give a go to serialize and unserialize:
1) Before sending your array, serialize it (and set your transfer mode to binary):
(...)
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // need this to post serialized data
curl_setopt($ch, CURLOPT_POSTFIELDS, serialize($array)); // $array is my above data
2) When you receive the data, unserialize it:
$array = unserialize($_POST);
More details here and here
$param['sub_array'] = json_encode($sub_array);
and on the other side
$sub_array= json_decode($_POST['sub_array']);