Hello may i know how to retrieve data in string?
$url = 'test url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_getinfo($ch);
$response = curl_exec($ch);
curl_close ($ch);
echo 'Response: ';
echo gettype($response);
echo '<br>';
echo($response);
Output :
Response: string
TRANSACTION_ID=abc123
MERCHANT_ACC_NO=M213213
TXN_STATUS=A
TRAN_DATE=2020-07-20
CAPTURE_DATE=2020-07-20
SALES_DATE=2020-07-20
RESPONSE_CODE=1
RESPONSE_MESSAGE=Success
As you can see the output of the code is as shown above. This is my first time to encounter this kind of output because usually I get json as the output. So my question is may I know how to retrieve the RESPONSE_MESSAGE in the output or may I know how to convert the output to array or json so that I can easily retrieve the data. Sorry for asking this I'm quite new with this PHP and CURL function.
You can explode to lines and explode the lines to parts in a foreach.
Edit: I realized the "Response:" was actually outputted manually.
Changed the code to slice the array from second item instead.
foreach(array_slice(explode("\n", $str), 1) as $line){
$temp = explode("=", $line);
$res[$temp[0]] = $temp[1];
}
Output:
array(8) {
["TRANSACTION_ID"]=>
string(6) "abc123"
["MERCHANT_ACC_NO"]=>
string(7) "M213213"
["TXN_STATUS"]=>
string(1) "A"
["TRAN_DATE"]=>
string(10) "2020-07-20"
["CAPTURE_DATE"]=>
string(10) "2020-07-20"
["SALES_DATE"]=>
string(11) "2020-07-20 "
["RESPONSE_CODE"]=>
string(1) "1"
["RESPONSE_MESSAGE"]=>
string(7) "Success"
}
https://3v4l.org/evdMO
Related
I have a curl request, I'm trying to get to display the 'name' from the JSON result in PHP but can't find anything about it all requires Jquery and AJAX.
<?php
$url="https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/alphamonkey95?api_key="myapikey";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
?>
<html>
<body>
<div>
<p>Display name here</p>
I'm guessing the echo['name'] Well tried that but it didnt work.
</div>
</body>
</html>
Results in JSON which is displayed on the page. I only want to display certain results.
array(6) { ["id"]=> int(85169216) ["accountId"]=> int(226919821) ["name"]=> string(13) "AlphaMonkey95" ["profileIconId"]=> int(3233) ["revisionDate"]=> int(1522708809000) ["summonerLevel"]=> int(73) }
If I understand you correctly:
$json = json_decode($result, TRUE); //return an array
var_dump ($json['name']);
I prefer objects myself:
$json = json_decode($result);//return an object
var_dump($json->name);
When you convert arrays back and forth JS doesn't have associative arrays, so it will convert non-numeric arrays to objects.
When making a curl post to another script, sometimes I may want to send null as the value for certain keys:
$postdata = array(
'userID' => 0,
'questionText' => "Can you answer this question?",
'time' => null);
$req = curl_init($url);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($req, CURLOPT_RETURNTRANSFER, false);
curl_setopt($req, CURLOPT_VERBOSE, true);
$data = curl_exec($req);
curl_close();
However, the data seems to have changed by the time it is received. Doing a vardump on $_POST reveals that the 'time' value is now an empty string:
array(3) {
["userID"]=>
string(1) "0"
["questionText"]=>
string(29) "Can you answer this question?"
["time"]=>
string(0) ""
}
What's going on here? Why is everything a string? How can I perserve the typing?
As comments to the original question state, HTTP requests will only ever provide strings.
I'd like to get JSON response on my second server with the domain example.com where the getit.php file contains:
$arr = array('antwort' => 'jo');
echo json_encode($arr);
Now when I try to get it with my first server:
$url = 'http://www.example.com/getit.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$antwort = curl_exec($ch);
curl_close($ch);
$antwort = json_decode($antwort);
echo '<pre>';
var_dump($antwort);
echo '</pre>';
I get:
object(stdClass)#2 (1) {
["antwort"]=>
string(4) "jo"
}
And not an normal array, how do I convert this to array?
I allready tried $antwort = json_decode(json_encode($antwort), True); but I only get a weird string with that?!
JSON's definition is: JavaScript Object Notation. JavaScript arrays can only have numeric keys. Your array has string keys, therefore it MUST be encoded as a JavaScript OJBECT, which do allow string keys.
Tell json_decode() you want arrays instead:
$arr = json_decode($json, true);
^^^^
as per the docs
Where how did you do your json_decode(json_encode($antwort))? The ONLY way that'd return a string is if you encoded the $antwort you'd gotten from curl_exec().
I'm trying to parse a JSON answer from Redmine API and I don't know how to get to the parts of the array.
Here is the code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://login:password#redmine.server/redmine/issues.json?cf_2=12345');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$data = json_decode($response);
When I make a var_dump($data), the answer looks like this:
array(1) { [0]=> object(stdClass)#1853 (14) { ["id"]=> int(96) ["project"]=> object(stdClass)#1852 (2) { ["id"]=> int(68) ["name"]=> string(7) "Test.......
So, when I make a for loop, I would like to access the parts of the array:
foreach($data as $issues){
var_dump($issues["id"]);
}
And so on. Any idea on this?
Stupid me...
The culprit was here:
$data = json_decode($response);
Should be:
$data = json_decode($response,true);
Now I get a proper PHP array.
I am using cURL to get json information from a site that pulls a random tumblr picture from a list of sources and I am interested of putting the json data retrieved into php variables so I can call for example, just the image url
$url = "http://someurl.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$a = curl_exec($ch);
$json = var_dump(json_decode($a, true));
I get:
array(3) {
'image' =>
array(1) {
[0] =>
string(62) "http://picture.jpg"
}
'source' =>
string(31) "http://source.tumblr.com"
'page' =>
string(9) "/page/164"
}
What would I now do in order to just print the url for the image?
I have tried
$url = $json["image"][0];
and then calling $url, but it gives me nothing in return. What I am doing wrong?
I have never worked with json before so I am at a loss here, any help is appreciated!
according to code looks:-
try
change
$json = var_dump(json_decode($a, true));
to
$json = json_decode($a, true);