Ok, i thinked i was able to do this easily but i cant figure how.
I have tried in many ways i found here and in other pages but nothing works; i spent two days with this, but i finally decided to register here and ask.
So here is my karma:
http://api.bitcoinvenezuela.com/DolarToday.php?json=yes
That url generates this json:
{"_antibloqueo":{"mobile":"https://dt.wordssl.net","video":"","corto_alternativo":"https://bit.ly/venezuela911","enable_iads":"","enable_admobbanners":"ca-app-pub-8212448379596570/1946229161","enable_admobinterstitials":"","alternativo":"68747470733a2f2f64626d7a647431663034653531632e636c6f756466726f6e64742e6e6574","alternativo2":"68747470733a2f2f64626d7a647431663034653531632e636c6f756466726f6e64742e6e6574","notifications":"https://d3c134ru0r3b0g.cloudfront.net","resource_id":"33504 A"},"_labels":{"a":"DOLARTODAY","a1":"DOLAR CUCUTA","b":"IMPLICITO","c":"DICOM","d":"DOLAR BITCOIN","e":"DIPRO"},"_timestamp":{"epoch":"1513837984","fecha":"Diciembre 21, 2017 02:33 AM","fecha_corta":"Dic 21, 2017","fecha_corta2":"Dic 2017","fecha_nice":"Diciembre 21, 2017","dia":"Jueves","dia_corta":"Jue"},"USD":{"transferencia":121205.72,"transfer_cucuta":121205.72,"efectivo":10330.84,"efectivo_real":122608.7,"efectivo_cucuta":122608.7,"promedio":121205.72,"promedio_real":11311,"cencoex":10,"sicad1":94496.22,"sicad2":11311,"bitcoin_ref":94496.22,"localbitcoin_ref":94496.22,"dolartoday":121205.72},"EUR":{"transferencia":143581.51,"transfer_cucuta":143581.51,"efectivo":12259.61,"efectivo_real":145502.19,"efectivo_cucuta":145502.19,"promedio":143581.51,"promedio_real":13422.76,"cencoex":11.87,"sicad1":112138.66,"sicad2":13422.76,"dolartoday":143581.51},"COL":{"efectivo":0.023,"transfer":0.023,"compra":0.023,"venta":0.02},"GOLD":{"rate":1265.9},"USDVEF":{"rate":10.02959},"USDCOL":{"setfxsell":2820,"setfxbuy":2740,"rate":3606,"ratecash":2825,"ratetrm":3005.76,"trmfactor":0.2,"trmfactorcash":0.06},"EURUSD":{"rate":1.18672},"BCV":{"fecha":"1513569600","fecha_nice":"Diciembre 18, 2017","liquidez":"100.643.049.794","reservas":"9.742.000"},"MISC":{"petroleo":"56,14","reservas":"9,7"}}*
I need read the json it generates to get the bolded values (only 2 values i need from there); I have tried with curl, file_get_contents, and many other ways, but nothing works.
Basically, I can not understand WHY this code do not works:
<?php
$url = 'http://api.bitcoinvenezuela.com/DolarToday.php?json=yes';
$obj = json_decode(file_get_contents($url), true);
echo $obj;
?>
I get a warning:
Warning: file_get_contents(http://api.bitcoinvenezuela.com/DolarToday.php?json=yes): failed to open stream: Connection timed out in /home/u704982448/public_html/lbapi.php on line 3
Any idea what is happening here? Where is the fail?
EDIT: Looks like the problem here is that my server ip is blocked. Any workaround for this?
Thanks to everyone.
You are trying to print an array in echo statement so its an error.
<?php
$url = 'http://api.bitcoinvenezuela.com/DolarToday.php?json=yes';
$obj = json_decode(file_get_contents($url), true);
echo "<pre>";
print_r($obj);
?>
Regarding connection timeout you need to check IP block etc
It works fine for me.
I tested it on my webpage and it worked fine.
Just needed to replace echo with var_dump().
http://www.hoppvader.nu/Mail/test.php
$url = 'http://api.bitcoinvenezuela.com/DolarToday.php?json=yes';
$obj = json_decode(file_get_contents($url), true);
var_dump($obj);
To echo the two values you need you use this code:
echo $obj["_timestamp"]["epoch"];
echo $obj["USD"]["transferencia"];
Seems to be a connection problem on server side. Maybe there is some firewall configured or the server has no connection to the internet etc...
You should try the following, log in into server and try to run:
curl http://api.bitcoinvenezuela.com/DolarToday.php?json=yes
This command would basically try to reach the url and print the output.
If you have the same timeout issue here then you have the proof that the error is independent of your php script.
Related
I´m trying a very simple php script which is about calling a json data through api calling on a online https link using MAMP.
However if I use the following code I have blank results:
<?php
$cnmkt = "https://api.coinmarketcap.com/v1/ticker/?limit=50";
$json = file_get_contents($cnmkt);
$fgc = json_decode($json,true);
echo $fgc[1]['percent_change_7d'];
?>
But if i copy/paste the content of the https link into a test.json file locally, substituting the https link with the test.json file on $cnmkt variable, the same exact script works properly.
I know i´m missing something very obvious, if someone could help me that would be very much appreciated, thanks.
Stefano
The script is working fine. I get an expected result of 4.63
Disable your AV/firewall and check again.
There's an extremely simple example of using Youtube API in php that somehow fails to work in my case, and I couldn't find a definitive solution for.
I desire to use the Youtube API without any wrapper, by myself, to get data of videos. The following search:list query works perfectly when I try accessing it from within by browser (as a link), but in php, I get that error when I try the same.
$apikey_YT = <my API key>;
$ytrequrl = "https://www.googleapis.com/youtube/v3/search?".
"part=snippet".
"&maxResults=50".
"&q=doom".
"&relatedToVideoId=h8j2zj-A5tE".
"&type=video".
"&key=${apikey_YT}";
$result = file_get_contents($ytrequrl);
var_dump($result);
The potential issues were URL encoding, or allowing allow-url-fopen, but neither seemed to help in my case: the former actually gave a new error message: No such file or directory.
What can I do?
Try it like this. Might be your formatting.
This works for me.
$ytrequrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&q=doom&relatedToVideoId=h8j2zj-A5tE&type=video&key='.$apikey_YT;
$info= file_get_contents($ytrequrl);
$info= json_decode($info, true);
print("<pre>".print_r($info,true)."</pre>");
I´m requesting an API like so (Domain is NSFW):
$external_videoid = "ph57fbb82bd33ab";
$string = file_get_contents("http://www.pornhub.com/webmasters/video_by_id?id=".$external_videoid."&thumbsize=big");
$json = json_decode($string, true);
$title = $json["video"]["title"];
echo $string;
But I can´t retrieve any data. $title is empty. The echo gives me:
<html><head>[...]<bodyonload="go()">Loading...</body>[...]</html>
Which might indicate that the JSON I want to retrieve is somehow loading, I guess. Seems pretty clear to me. It seems like file_get_contents has to wait for the site to load. What can I do?
APPEND: Sometimes the request works. Sometimes it doesn´t.
Your code works for me, $title echos as:
I Feel Myself Creature Comforts 1
(lol)
I suppose the connection was ACTUALLY loading when you tried it. The json returns some highly "entertaining" information haha, especially the tags (NSFW obviously)
Be aware pornhub has an API(!): http://www.hubtraffic.com/
Use this code.
$v= "ph57fbb82bd33ab";
$url="http://www.example.com/sample/param?id=".$v;
$data = file_get_contents($url);
$characters=json_decode($data, true);
echo $characters['video']['title'];
I'm trying to learn reddit code and using this https://github.com/jcleblanc/reddit-php-sdk
There's a method in this library $reddit->getUser() which outputs null always. Everything else seems to be working though.
include_once("jcleblanc-reddit-php-sdk-4a4cc32/reddit.php");
$reddit = new reddit("my-username", "my-password");
$userData = $reddit->getUser();
var_dump($userData);
outputs null.
$response = $reddit->getListing("all", 5);
var_dump($response);
outputs what's expected just fine. Then what could be going wrong with $reddit->getUser()?
this is Jonathan LeBlanc, I created that library. I'm running tests right now and I don't seem to be seeing the same issues. This is the code I'm running with the library:
<?php
require_once("reddit.php");
$reddit = new reddit("USERNAME", "PASSWORD");
var_dump($reddit->getUser());
?>
When running that, I am seeing my user data object coming through. Are you still experiencing this issue?
Jon
I'm sure this is similar to other questions, but I have tried the solutions, and couldn't get it to work.
I'm trying to get the location of an IP address, decode the json, and print it. I actually plan to put it into a database, but Im not even sure why I cant get anything to print to the screen.
$ip = $_SERVER['REMOTE_ADDR'];
$json = file_get_contents("http://api.hostip.info/get_json.php?ip=".$ip);
$local = json_decode($json,true);
thanks.
A. When you mean I cant get anything to print to the screen. definitely you can get anything because you did not output anything to screen.
error_reporting(E_ALL);
$ip = $_SERVER['REMOTE_ADDR'];
$json = file_get_contents("http://api.hostip.info/get_json.php?ip=".$ip);
$local = json_decode($json,true);
echo $local['city'] ;
print $local['country_name'] ;
var_dump($local);
B. It would also mean that allow_url_include set to Off which would not allow file_get_contents load a information from another domain
Firstly, the code you've provided does not actually print anything, which would explain why you're getting a blank screen. I assume you're doing something like echo $local; later in the program, but if not, that'll be the problem.
Assuming that isn't the issue....
Two possibilities:
file_get_contents() is returning an error.
json_decode() is returning an error.
Firstly, you should add some additional error trapping in your code to determine which one is the problem.
If you have PHP set to not display errors, consider switching them on so you can see any errors that are generated. Or else look at your server log to see the errors.
file_get_contents() may fail if the URL is invalid. It may also fail if your PHP installation is set to not allow URLs via the file handling functions. If this is the case you'll need to change your php.ini settings. If you can't do that, you'll have to load the file via a different method (eg Curl).
json_decode() is only likely to fail if the string it gets is invalid JSON. You should check the output of the URL to confirm that it looks right, and check it something like firebug to ensure it is valid.
Hope that helps.