Json returns null value - php

I want to get Json data from Carquery API. But none of my code worked.
First i tried to get the data using curl. This is my code
$loginUrl = 'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$loginUrl);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));
It returned null. And then i tried using file_get_content. This is my code :
$json = file_get_contents('http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015');
$obj = json_decode($json);
var_dump($obj);
Unfortunately it didn't work either.
Any help please?

First of all try this:
$loginUrl = 'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$loginUrl);
$result=curl_exec($ch);
if($result === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
var_dump(json_decode($result));
}
curl_close($ch);
so you can see error if any on curl request.
and 2nd thing that url:
'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015'
are you sure that the must be 2 (two) ? (question marks) ?

json data return from that API is not correct, i tried using jsonlint.com to check it.
If the callback part is removed from
'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
into
'http://www.carqueryapi.com/api/0.3/?cmd=getTrims&year=2015';
it will produce right json and can be parsed either using file_get_content() or curl.

Related

PHP Simple Api post request

Hi there I never develop in php but I have to create a small function to do a post request to my meteor node application. The end point works fine I have been testing it. I am trying to use the post request to get my login token back. Providing username and password it should return json data even it is incorrect it returns json data saying login refused.
It seem noting is happening tho. My return data seems to always be null.
As I said I never really used php nore do I plan on using it much. But here is my code probably very easy mistake to fix.
<?php
$ch = curl_init();
$params = array(
"username" => "apilogin",
"password" => "12345"
);
echo httpPost("http://127.0.0.1:3000/api/login", $params);
function httpPost($url,$params)
{
$postData = http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$output=curl_exec($ch);
// Check for errors
if($output === FALSE){
echo "false";
die(curl_error($ch));
}
// Decode the response
$output = json_decode($response, TRUE);
curl_close($ch);
return $output;
}
?>
change:
$output = json_decode($response, TRUE);
into:
$output = json_decode($output, TRUE);
you never made a variable $response

JSON to PHP Array error

i am having trouble getting a JSON file to a php-array.
i got a json-file as response from an api (request done with curl)
and want to make an array out of it but it won't work.
Here is my code:
<?php
class modExpose{
public static function getFunction($id){
//In my code i am "preparing" the request here
// *********** cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.$qry_str);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}
$id = $_GET['id'];
$data = modExpose::getFunction($id);
$array = json_decode($data,true);
print_r($array);
?>
the print_r function only delivers: 1. (same does the var_dump() function).
I also tried adding html_entity_decode() but the problem still remains.
Thank's for helping!
That is probably because the return value of your curl_exec() call is true on success and that is all you are returning from your method.
If you want to get the data that was returned by the curl call, you need to set the CURLOPT_RETURNTRANSFER option:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.$qry_str);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// Return the result on success
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
// Now response will contain the results of your curl call
$response = curl_exec($ch);
Apart from that I assume you have checked the variables that seem to be undefined in your example code.

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;

Getting data from Json url using CURL Returning NULL

i have a JSON link , i want to get data from it but it is returning NULL.
Code:
$url = 'https://cheddargetter.com/json/customers/search/orderBy/id/orderByDirection/asc/perPage/100/';
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
//var_dump(json_decode($result, true));
echo '<pre>abc'. print_r($result) . '</pre>';
your code looks correct. the problem is the url. I guess it ask for authentication to get the json data.
https://cheddargetter.com/?fwd=%2Fjson%2Fcustomers%2Fsearch%2ForderBy%2Fid%2ForderByDirection%2Fasc%2FperPage%2F100%2F#login

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.

Categories