twitter search api not returning results - php

i'm doing api calls from php backend like this:
$term = "AT&T Park";
$q = '"'. urlencode($term) .'"';
$url = "http://search.twitter.com/search.json?q={$q}&rpp=100";
$api_call = file_get_contents($url);
it's not returning anything, but same api call working just fine on my terminal.
here is a little screencast showing what's going on:
http://screencast.com/t/RnZsrwfGcc2q
any ideas?

you need to urlencode the quotes too.
change your code to the following
$term = '"AT&T Park"';
$q = urlencode($term);
$url = "http://search.twitter.com/search.json?q={$q}&rpp=100";
$api_call = file_get_contents($url);
and it should work

You can use cURL in your PHP file. This may work.
http://us3.php.net/manual/en/function.curl-init.php

Related

PHP Parse URL From Current URL

I am trying to parse url and extract value from it .My url value is www.mysite.com/register/?referredby=admin. I want to get value admin from this url. For this, I have written following code. Its giving me value referredby=admin, but I want only admin as value. How Can I achieve this? Below is my code:
<?php
$url = $current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
setcookie('ref_by', parse_url($url, PHP_URL_QUERY));
echo $_COOKIE['ref_by'];
?>
You can use parse_str() function.
$url = "www.mysite.com/register/?email=admin";
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['email'];
Try this code,
$url = "www.mysite.com/register/?referredby=admin";
$parse = parse_url($url, PHP_URL_QUERY);
parse_str($parse, $output);
echo $output['referredby'];
$referred = $_GET['referredby'];
$referred = "referredby=admin";
$pieces = explode("=", $referred);
echo $pieces[1]; // admin
I don't know if it's still relevant for you, but maybe it is for others: I've recently released a composer package for parsing urls (https://www.crwlr.software/packages/url). Using that library you can do it like this:
$url = 'https://www.example.com/register/?referredby=admin';
$query = Crwlr\Url\Url::parse($url)->queryArray();
echo $query['referredby'];
The parse method parses the url and returns an object, the queryArray method returns the url query as array.
Is not a really clean solution, but you can try something like:
$url = "MYURL";
$parse = parse_url($url);
parse_str($parse['query']);
echo $referredby; // same name of the get param (see parse_str doc)
PHP.net: Warning
Using this function without the result parameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2.
Dynamically setting variables in function's scope suffers from exactly same problems as register_globals.
Read section on security of Using Register Globals explaining why it is dangerous.

JSON Decode PHP (Curl vs File_Get)

Currently, I am trying to use the Twitch API to grab Twitch stats such as current viewers, Title, and more I am running into an issue with #File_Get_Contents when using this my request seem to be delayed or not getting them as quick as I refresh, i.e. I think the results may be cache'd.
For example here is my old code
$twitch = json_decode(curl_get_file_contents('https://api.twitch.tv/kraken/channels/'.$twitch_channel), true);
$display_name = $twitch['display_name'];
$game = $twitch['game'];
$status = $twitch['status'];
$url = $twitch['url'];
$avatar = $twitch['logo'];
$views = $twitch['views'];
$followers = $twitch['followers'];
The main issue with this is it didnt seem like it updated every time I refreshed, so I looked into using cURL for better results + I heard it's much quicker with load time!
Here is my current curl code
$requesturl='https://api.twitch.tv/kraken/channels/' . $twitch_username;
$ch=curl_init($requesturl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cexecute=curl_exec($ch);
curl_close($ch);
$twitch = json_decode($cexecute,true);
$display_name = $twitch['display_name'];
$game = $twitch['game'];
$status = $twitch['status'];
$url = $twitch['url'];
$avatar = $twitch['logo'];
$views = $twitch['views'];
$followers = $twitch['followers'];
Your log is showing PHP Notice, You don't have any error. I change a lil bit your code to test it and It's working. So you're probably just no printing your vars.
Check your code with a print_r online
So ultimnately I figured out what the issue was, it's XAMP Curl not working for whatever reason. It's setup properly in the PHP.ini, I have the Two .DLL in my Sys32. So I dont know why this isn't working

get request for a website in a php script

I want to get request in a website like that
$id = "something";
$response = http_get("http://example.com?id=$id, array("timeout"=>1), $out);
I don't find how exactly I can use http_get
I've tried with file_get_contents, it is working like
$out = file_get_contents("http://www.example.com?id=something");
but I want it like that
$out = file_get_contents("http://www.example.com?id=$id");
this one is not working
The way I got it to work was using the code below.
$out = file_get_contents("http://www.example.com?id=".$id);
This will concatenate the $id variable contents onto the end of the url to request.

Request url on a save way

I would like to read a url. www.domain.com?cookie=set&redirect=yes
Now I want to use $ _SERVER['REQUEST_URI'] but this does not work with strip_tags and htmlspecialchars.
Also many I read that you should watch for XSS.
Does anyone know how to save a URL can be used by GET?
$url = "http://'.$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI];
$url = strip_tags($url);//doesnt work
$url = htmlspecialchars($url);//doesnt work
Thanks!
Edit to (doesnt work):
$url = "http://".$_SERVER[HTTP_HOST]."".$_SERVER[REQUEST_URI];
$url = strip_tags($url);
echo $url;
for example
www.domain.com?cookie=set&redirect=yes
output => index.php?cookie=se%3Cscript%3Et&re%3Cb%3Ed%3C/b%3Eirect=yes
This line
$url = "http://'.$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI];
Needs to be either
$url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
or
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
The way you are currently doing it will not concatenate the data correctly.
Issues with the your line:
Your mixing quotes around the protocol " to open and ' to close
You are not quoting the $_SERVER params e.g $_SERVER['PARAM']
You are not joining the 2 $_SERVER vars with anything so you'll get a syntax error

A weird result, as a result of this PHP script

require_once 'include/BestBuy/Service/Remix.php';
$skuid = rawurldecode($_GET['skuid']);
$apiKey = 'tfvs7h89pwn4pzmyj9nxemmg'; // Your API key
$remix = new BestBuy_Service_Remix($apiKey);
$result = $remix->product('$skuid','json')
->show(array('url'))
->query();
$data = json_decode ($result, true);
$feed = $data['url'];
print <<< FEEDS
$feed
FEEDS;
When I put this script into my page, the $feed will echo the current URL.
But when I manually supply the script with an integer, replacing ($skuid) it will be successful.
It's really weird, But I think it has something to do with me using a variable in that specific array.
And it is also weird, because It was working before I re arranged some of the HTML.
I'm trying to approach this problem the most logical way.
Please help.
Thanks.
should you have $skuid in quotes? I would expect:
$result = $remix->product($skuid,'json')
rather than
$result = $remix->product('$skuid','json')

Categories