API : How to put useragent into url GET? - php

In php I would like to use API like this : website.com/api.php?ip=$ip&ua=$useragent.
However, with the useragent, variable $response return nothing (it displays nothing as if it was null).
I had tried with only IP address alone and it works fine.
My code :
$useragent = $_SERVER['HTTP_USER_AGENT'];
$ip = $_SERVER['REMOTE_ADDR'];
$query = 'https://website.com/api.php?ip='.$ip.'&page='.$useragent;
$response = file_get_contents($query);
echo $response;
?>
I think it's because of the characters in useragent, do you have any idea how to fix it?
Thanks

Use the urlencode() function to encode special characters.
And &page= should be &ua=.
$query = 'https://website.com/api.php?ip='.$ip.'&ua='.urlencode($useragent);

Related

Returning value from restAPI in php, echo value not showing

I'm trying to return a value from my API using PHP, api can be found here:
code is as follows:
Im not seeing the echo on my page, don't see any errors and I believing im reading the json in correctly. Any help appreciated!
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$returnvalue = json_decode($response, true);
echo $returnvalue["Age"];
?>
From what I can tell, the json is not valid on the server side (due to the "connected to db" text in front of the {} part). I think it would be a good idea to fix the server side response json data, if possible!
For now, here is a way to get the value it looks like you are intending to retrieve:
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$adjusted_response = str_replace('connected to db', '', $response);
$returnvalue = json_decode($adjusted_response, true);
echo $returnvalue['tv_shows']['Age'];
?>
Output:
$ php example.php
16+
If the server side json data is fixed, I think you could shorten the code to something like this:
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$returnvalue = json_decode($response, true);
echo $returnvalue['tv_shows']['Age'];
?>
The thing is that $response is returned as string , in order to fix that you need to edit your backend and make it give the response without "connected to db"

file_get_contents() always returns bool(false) while URL returns a value during recapctha V2 upgrade in PHP

I am currently updating recaptchalib.php to upgrade captcha from V1 to V2.
I have done the client integration successfully. But at the server side facing issues. In my below code snippet if I hit the URL directly in browser I either get
"success" : false with "error-codes" as "timeout-or-duplicate" OR
"success" :true but in both the above cases file_get_contents() returns the value as bool(false). Ultimately, always I get the error as "reCaptcha not enetered correctly" .Please suggest why file_get_content() is always returning bool(false)? What correction can be done in code so that we get response in correct format and then do json_decode successfully i.e hit 'true' case .
Below is the code snippet
$postdata = http_build_query(
array(
'secret' => $privkey,
'response' => $response,
'remoteip' => $remoteip
)
);
$url = 'https://www.google.com/recaptcha/api/siteverify?'.$postdata;
print "URL is " . $url . "<br>";
$response = file_get_contents($url);
var_dump($response) ;
$responses = json_decode($response, true);
if($responses["success"] === TRUE){
echo "true";
}else{
echo "false";
}
For file_get_contents() to work, the URL that is passed must not conatin any special characters. To obtain the sanitized version of the URL, use urlencode() as mentioned here. Looks like your URL might have special characters (private key etc.).
Try this:
$url = 'https://www.google.com/recaptcha/api/siteverify?'.$postdata;
$url = urlencode($url);
$response = file_get_contents($url);

Unable to send URL

I am using CURL to send a URL. It's not sending an URL which contains PHP variables although is working perfectly on a defined URL (not incuding any PHP variable).
For example: This link is not working because of PHP variables :
$url = "http://abc/create/name/{$firstname} {$lastname}/email/{$email}/password/{$password1}?level={$level}&session=Dec";
$request = curl_init($url);
$response = curl_exec($request);
var_dump($response);
This works fine: Contains static values
$url = "http://abc/create/name/any one/email/anyone#gmail.com/password/12345?level=1&session=Dec";
$request = curl_init($url);
$response = curl_exec($request);
var_dump($response);
What am I doing wrong? Any leads?
Note: The URL is perfectly echoed, No error in echo $url
please putt like this and it will work
$url = 'http://abc/create/name/{'.$firstname.'}{'.$lastname.'}/email/{'.$email.'}/password/{'.$password1.'}?level={'.$level.'}&session=Dec"';
Finally got the solution :)
I am giving a space between in my {$firstname} {$lastname}thats why its not sending the URL.
So i just concatenate my firstname and lastname in a single variable and it works like a charm.
$fullname = $get_user_firstname.$get_user_lastname;
$fullname = urlencode($fullname);
$url = "http://abc/create/name/{$fullname}/email/{$email}/password/{$password1}?level={$level}&session=Dec";
Hope anyone with similar issue can get help from my this answer that's why i posted it. cheers!

PHP - Getting a URL within a function argument

I have the below function that works perfect when I put the URL string within the argument manually. I need it to be dynamic though and I am using Wordpress.
function get_tweets($url) {
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
// Below is the one that works manually
<?php echo get_tweets('http://www.someurl.com');
//ones I have tried that do not (trying to make dynamic)
$url = $get_permalink();
echo get_tweets('$url');
echo get_tweets($url);
$url = '$get_permalink()';
$url = $get_permalink(); // produces needs to be in string error
echo get_tweets($url);
There is nothing wrong with what you're doing, per se. The only obvious mistake I can see is that you aren't encoding the URL properly. You need to ensure the query string arguments you put in the URL are properly URL encoded, otherwise the remote host may not interpret the request correctly.
function get_tweets($url) {
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . urlencode($url));
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
echo get_tweets('http://www.someurl.com'); // should work just fine
Did you try to urlencode your url String?
urlencode($foo);
Your main problem is on below line
Change
//ones I have tried that do not (trying to make dynamic)
$url = $get_permalink();
To
//ones I have tried that do not (trying to make dynamic)
$url = get_permalink();

Get user's name from Facebook Graph API

I would like to know how is it possible to retrieve a string from an external page.
For example: In a PHP website, the user sends a facebook id, ex: 1157251270
And the website returns the name from http://graph.facebook.com/1157251270.
I hope I made it clear.
Thank you
The Graph API returns JSON strings, so you can use:
echo json_decode(file_get_contents('http://graph.facebook.com/1157251270'))->name;
or more verbose:
$pageContent = file_get_contents('http://graph.facebook.com/1157251270');
$parsedJson = json_decode($pageContent);
echo $parsedJson->name; // Romanos Fessas
See json_decode — Decodes a JSON string
If you are using Facebook's PHP SDK, you can also do this to query their graph API:
$fb = new Facebook();
$object = $fb->api('/1157251270');
you get it by:
$link = json_decode(file_get_contents('http://graph.facebook.com/1157251270'));
echo $link->name;
Nice tut:
http://webhole.net/2009/08/31/how-to-read-json-data-with-php/
Either you use :
$res_json = file_gets_contents("http://graph.facebook.com/1157251270")
$res = json_decode($res_json)
Or, if you prefer curl (here with https and access token) :
$ch4 = curl_init();
curl_setopt($ch4, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch4, CURLOPT_URL, "https://graph.facebook.com/1157251270?access_token=YOUR_ACCESS_TOKEN");
curl_setopt($ch4, CURLOPT_SSL_VERIFYPEER, false);
if(!$result = curl_exec($ch4))
{
echo curl_error($ch4);
} else {
$res = json_decode($res_json)
}
curl_close($ch4);
For facebook data you can use json_decode.
For another sites try with webscraping, for example:
here

Categories