php save value in cURL response - php

I made a cURL to Salesforce and got a responce like:
{"access_token":"XXXXXXXXX","instance_url":"https://na25.salesforce.com","id":"XXXXXXXXX","token_type":"Bearer","issued_at":"1458059021745","signature":"XXXXXXXX="}
I am trying to save the "access_token" like so but not working:
$result = curl_exec($ch2);
$session_id = $result['access_token'];
I have also tried:
$result = curl_exec($ch2);
$parsed = array();
parse_str($result, $parsed);
$session_id = $parsed['access_token'];

the API is returning a json string. you need to run it through json_decode first.
$result = curl_exec($ch2);
$resultArray = json_decode($result, true);
$session_id = $resultArray['access_token'];

you can try with below code
$result = curl_exec($ch2);
$result_aray = json_decode($result,true);
$session_id = $result_aray ['access_token'];
Let me know still anything.

Related

How to Json In PHP 'iyas'

http://simsimi.com/getRealtimeReq?uuid=VbQqwsQ3qiCw1T04VrrFQzBBlkfZibg3YFkbWz6VquZ&lc=id&ft=1&reqText=Eh+jelek&status=W
How to get value from respSentence
Im using php lang ^_^
" {"status":200,"respSentence" 11 \:"respSentence"}"` is not valid json. Use some online sites to validate your json and try like this,
$json = '{"status":200,"respSentence" :"iyas"}';
$array = json_decode($json, true);
echo $array ['respSentence'];
You can try using file_get_contents and json_decode PHP functions
$url = "http://simsimi.com/........";
$data = file_get_contents($url);
$json = json_decode($data, true);
echo $json ['respSentence'];
hope it helps

Get location from a stream url in PHP

I'm trying to get the redirect url from a stream using php.
Here's the code I have right now:
<?php
$stream = 'https://api.soundcloud.com/tracks/178525956/stream?client_id=XXXXXX';
$ch = curl_init($stream);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = curl_exec($ch);
echo $url;
curl_close($ch);
?>
Which outputs as a string:
{"status":"302 - Found","location":"THE_URL_I_WANT"}
So how ould I go about getting the url I want as a variable?
Thanks
It's simple use json_decode
$data = json_decode($url);
$your_url = $data->location;
How about
$data = json_decode($url);
$location = $data["location"];

How to send JSON and retrieve it using cURL?

This link = http://localhost/api_v2/url?key=***
will return this list of JSON :
I've already tested by :
Navigate to http://localhost/api_v2/url?key=*** through a browser
And make a curl request via command line $ curl http://localhost/api_v2/url?key=***
either way will give return the JSON, and give me the same result.
Well, what I can tell by that is my $array is storing something in it.
Even if I did dd($array) - I still get the same result.
Here is what I've
Here is how I establish my JSON
public function index_2(){
$file_name = 'inventory.csv';
$file_path = 'C:\\QuickBooks\\'.$file_name;
$csv= file_get_contents($file_path);
$utf8_csv = utf8_encode($csv);
$array = array_map("str_getcsv", explode("\n", $utf8_csv));
return Response::json($array);
}
Here is how I make a cURL request and trying to retrieve that JSON
<?php
$ch = curl_init("http://localhost/api_v2/url?key=***");
curl_setopt($ch, CURLOPT_USERPWD, "admin:*****");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$body = curl_exec($ch);
curl_close($ch);
$json_decode = json_decode($array, TRUE);
I keep getting complaint that $array variable is not define, but in fact I did define and send it over like this return Response::json($array); .
I am not sure what I did wrong here.
Spot the difference:
$body = curl_exec($ch);
^^^^^
$json_decode = json_decode($array, TRUE);
^^^^^^

Having trouble loading php curl results

So I'm having a problem with the following code.
I've got CURLOPT_RETURNTRANSFER set to true, yet nothing is returned when curl_exec is hit. Any and all help is appreciated!
<?php
$yql_base_url = "http://query.yahooapis.com/v1/public/yql?q=";
$yql_query = "select * from csv where url='http://download.finance.yahoo.com/d/quotes.csv?s=YHOO,GOOG,AAPL&f=sl1d1t1c1ohgv&e=.csv' and columns='symbol,price,date,time,change,col1,high,low,col2'";
$yql_params = "&format=json&diagnostics=true&callback=";
$yql_url = $yql_base_url . urlencode($yql_query) . $yql_params;
$session = curl_init($yql_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($session);
curl_close($session);
$phpObj = json_decode($json);
if(!is_null($phpObj->query->results))
{
echo $phpObj->query->results;
}
?>
$phpObj->query->results is an array of Object and you can not do echo on it. Simply use print_r() or var_dump() on it.
Example:
print_r($phpObj->query->results);
var_dump($phpObj->query->results);

php doesn't get the json values i send from curl

curl -i -X POST -d 'json={"Amount":1000,"idPlayers":1}' http://www.myurl.com/updateamount.php
My php code:
<?php
$json = urldecode($_GET['jsonSendData']);
$json = str_replace("\\", "", $json);
$data = json_decode($json, true);
// if i hard code it here it works fine, but need from a post from curl
//$json = '{"Amount":1000,"idPlayers":1}';
$data = json_decode($json, true);
echo "json = ", $json;
$amount = mysql_real_escape_string($data['Amount']);
$id = mysql_real_escape_string($data['idPlayers']);
echo "Amount = ",$amount;
return;
Try to change to read post data and using the correct name:
$json = urldecode($_POST['json']);

Categories