a reddit-php-sdk method not outputting anything - php

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

Related

PHP: SimpleHTMLDom - Help me get the html page code?

who can help me implement getting html code from this page https://investmint.ru? I'm using the "SimpleHTMLDom" library, but something goes wrong and I get an empty response. Although other sites work fine with this code. What's wrong? Who can suggest or give advice?
include("simplehtmldom/simple_html_dom.php");
$url = "https://investmint.ru";
$html = new simple_html_dom();
$html->load_file($url);
echo $html;
I will be glad for any decision, thanks.
I get an empty response...Although other sites work fine with this code
I couldn't be bothered setting up the library, so I ran this....
print file_get_contents('https://investmint.ru');
I got this....
<html><head><script>function set_cookie(){var now = new Date();
var time = now.getTime();time += 19360000 * 1000;now.setTime(time);
document.cookie='beget=begetok'+'; expires='+now.toGMTString()+';
path=/';}set_cookie();location.reload();;</script></head><body></body></html>
I suspect you got the same but didn't investigate it properly.
Try it with the cookie. And learn how to use web developer tools in your browser.

Get a Json string from a public URL using php

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.

null answer with POST data CodeIgniter 2.1.3

I have this problem.
I used this sentence to get post info in CodeIgniter 1.7.2 and it works fine
function function1(){
$input_data = json_decode(trim(file_get_contents('php://input')), true);
$info = str_replace( '"', '', json_encode($input_data['info']));
}
My input json is this:
{
"info":"hello!"
}
But when I used these same lines in CodeIgniter 2.1.3 it doesn't work. I used echo $info but my answer is null. Anyone can help me? Where is the mistake?
This behavior is not CI related. It's most PHP concepts.
Your mistake is probably that you're trying to access something that doesn't exist.
What is the point to use file_get_contents('php://input')? Can't you just use $this->input->post(), or the uploadclass? (in CI context)?
Anyway, check what do you get in file_get_contents(), var_dumping it. I'm pretty sure that your NULL comes from $input_data assignment statment, so you're fetching the JSON in the wrong way at first.
I'm using a Controller as a WebService and It works fine in a previous work. So, I tested with this
function test(){
$m = $this->input->post('key');
echo $m;
}
and I'm sending a POST JSON data as follow:
{
'key':'hello!"'
}
and now I receive nothing. Some else?

How to send response using "new Response" or "Response::factory()" in Kohana 3.1?

The examples I see are suggesting the use like this:
$response = new Response();
echo $response->body($view)->send_headers()->body();
But if I have the following code
$response = new Response();
$response->headers('Content-Type', 'application/json');
echo $response->body($view)->send_headers()->body();
it sends the output allright, but it is in HTML format, not JSON.
Whats the valid way to send a new Response along with headers then? I am using Kohana 3.1 with php 5.3
Edit:
Before you start thinking what a fool I am for not using $this->response->body(<blah here>);, let me clarify that I know how to use the controller's response object... That is not what I am looking for... I want to send a response entirely from a static function that is not anywhere near a controller in the function call stack :)
Still new to Kohana but here's how I did it and it seems to work fine:
$this->response->headers('Content-type','application/json; charset='.Kohana::$charset);
$this->response->body(json_encode( $data_array ));
I'm thinkin charset is optional... hope this helps.
OK, after scratching my head and attempting many solutions, I decided to use this:
Request::initial()->response()
->body($view)
->headers('Content-type','application/json');
Works from anywhere unless you write exit somewhere in the flow.

Why is my string containing a URL breaking my script?

Using twitterauth to try to post status updates. This is my code (which returns a 403 error from twitter when I try to post it):
$fact = "This is a status update. http://onth.is/iss" ;
$parameters = array('status' => $fact);
However, if I do this:
$parameters = array('status' => "This is a status update. http://onth.is/iss");
It post perfectly fine. I know it has something to do wit the URL, because if I remove it from the first code it works.
Any tips? Thanks in advance!
If you are referring to the twitteroath library then I don't see anything wrong with your code. However, you can speed things up a little bit by doing:
$parameters["status"] = "This is a status update. http://onth.is/iss";
The two statements are identical, except that the latter will create a syntax error :)
My guess would be that you need to urlencode() the string before sending it to Twitter, but not knowing the library you are using, I can't say for sure.

Categories