Getting json response in php - php

Does Php have a method that calls a Json URL, and save in a variable the data it fetches
for example can I used:
"https://www.googleapis.com/blogger/v3/blogs/1/posts/"
I been trying with this code, but i am not sure if this is wrking
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/blogger/v3/blogs/1/posts/");
curl_setopt($ch, CURLOPT_HEADER, 0);
$df = curl_exec($ch);
curl_close($ch);

You can use the bulti-in function json_decode http://php.net/manual/it/function.json-decode.php
$json = file_get_contents("https://www.googleapis.com/blogger/v3/blogs/1/posts/");
$obj = json_decode($json); //returns an object of json values
$array = json_decode($json, true); //returns an array of json values

The problem is http*s*, try adding these:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

Related

I have created an call back API to receive the JSON data but its not working

I have created a call back API to receive the JSON data after hitting my server. But somehow its not working as expected
This the response which i have to receive in my API
{"ERROR":0,"STATUS":1,"ORDERID":753093,"OPTRANSID":"2107938600"}
I have written the php file for this rcstatus.php
$url = 'http://softwarecompany.club/pks/recharge/b2b/rcstatus.php?status='.$_GET['status'].'&Orderid='.$_GET['Orderid'].'&Optransid='.$_GET['Optransid'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
$data = curl_exec($ch);
echo $data;
$json = json_decode($url,true);
$json['STATUS'];
Please help me to achieve this. I have never worked with JSON data before. So please help me to achieve it. Thanks in advance!!!
You need to replace:
$json = json_decode($url,true);
with
$json = json_decode($data,true);
Replace $url to $data in last when you get the response like this.
$url = 'http://softwarecompany.club/pks/recharge/b2b/rcstatus.php?status='.$_GET['status'].'&Orderid='.$_GET['Orderid'].'&Optransid='.$_GET['Optransid'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
$data = curl_exec($ch); // This is Your Response
$json = json_decode($data,true);
$json['STATUS'];

How to get JSON parameter from URL

Hello I'm coming from Get JSON object from URL
I tried to get marketCap->usd.
Can anyone tell me what I'm doing wrong please?
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://coinmarketcap.northpole.ro/api/v5/ZCL.json');
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
echo $result->marketCap->usd;
Change echo $result->marketCap->usd; for echo $obj->marketCap->usd;
You have saved the data decoded in $obj.
Last line of your code should be
echo $obj->marketCap->usd;

Parsing object in PHP not working

I'm using this to get my response (only relevant code below):
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
$my_resp = $result['total_count'];
var_dump($my_resp);
echo $result gives me an object that appears to be valid JSON. Like this:
{
"total_count": 1
}
However, $my_resp comes back as NULL. What am I doing wrong here?
You need to turn it into an object from a string
$result = json_decode( curl_exec($ch));
or an associative array
$result = json_decode( curl_exec($ch), true );
If it is JSON, you should json_decode() it before trying to read fields.

How to retrieve only one string from a cURL response

I am using the following code to get information about a repo using the Github API.
I am using cURL, but I'm not sure how to just get the name of the repository. So, how can I get just one string from the response and echo that without echoing the complete response? I tried doing $data['name'] but that didn't work.
code:
$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT,'Content-type: application/json');
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/ruby/ruby');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
This request returns JSON, so just use json_decode.
$jsonStr = curl_exec($ch);
curl_close($ch);
$json = json_decode($jsonStr, true);
var_dump($json['name']);
var_dump($json['full_name']);
It should be trivial from here to get the elements you're interested in.

Get specific data from json result by using PHP

I am trying to get some json result by using curl. But I don't want to get all of the results. I just want to get a specific parameter from it. So what should I do?
This is my method:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_URL, "http://sample.jsp?id=123456");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
$parts = parse_str($output);
$memberid = mysqli_real_escape_string($con, $query['memberid ']);
I get this result:
{"memberid ":"000000000","cardNumber":"111111111","result":"OK"}
But actually I just want
000000000
Please help.
$json = json_decode("{"memberid ":"000000000","cardNumber":"111111111","result":"OK"}", true);
echo $json["memberid"];
Two options: either change the server so it sends only what you need, or parse the response and extract the data.

Categories