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!
Related
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"
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);
I want to pass a string from one PHP file to another using $_GET method. This string has different value each time it is being passed. As I understand, you pass GET parameters over a URL and you have to explicitly tell what the parameter is. What if you want to return whatever the string value is from providing server to server requesting it? I want to pass in json data format. Additionally how do I send it as Ajax?
Server (get.php):
<?php
$tagID = '123456'; //this is different every time
$tag = array('tagID' => $_GET['tagID']);
echo json_encode($tag);
?>
Server (rec.php):
<?php
$url = "http://192.168.12.169/RFID2/get.php?tagID=".$tagID;
$json = file_get_contents($url);
#var_dump($json);
$data = json_decode($json);
#var_dump($data);
echo $data;
?>
If I understand correctly, you want to get the tagID from the server? You can simply pass a 'request' parameter to the server that tells the server what to return.
EDIT: This really isn't the proper way to implement an API (like, at all), but for the sake of answering your question, this is how:
Server
switch($_GET['request']) {
case 'tagID';
echo json_encode($tag);
break;
}
You can now get the tagID with a URL like 192.168.12.169/get.php?request=tagId
Client (PHP with CURL)
When it comes to the client it gets a bit more complicated. You mention AJAX, but that will only work for JavaScript. Your php file can't use AJAX, you'll have to use cURL.
$request = "?request=tagID";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '192.168.12.169/get.php' . $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
echo $content;
EDIT: added the working cURL example just for completeness.
Included cURL example from: How to switch from POST to GET in PHP CURL
Client (Javascript with AJAX)
$.get("192.168.12.169/get.php?request=tagId", function(data) {
alert(data);
});
I'm trying to get some data from a website using PHP Curl as follows:-
$url_1 = "website.com"
$url_2 = "http://www.website.com"
$url_3 = "http://www." . $url_1;
$ch = curl_init($url_1); // failure
$ch = curl_init($url_2); // success
$ch = curl_init($url_3); // failure
I have a huge list of URLS in the format of $url_1 please will you let me know how I can add the http:// prefix to the url so it can be accepted by curl_init()
Thanks
Try $ch = curl_init("http://www." . trim($url_1));
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();