I m trying to generate a QR code with form values my problem is it's showing some error like:
Warning: file_get_contents(http://www.test.com/chart?chs=100x100&cht=qr&chl=test1,test2 , test3): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in local path.
When I m running this code it will generate a QR-code with no data.
So far I have done this:
$datas = $_POST['data'];
$width = $height = 100;
$url = implode(", ",$datas);
$qr_img = "<img src=\"http://www.test.com/chart?chs={$width}x{$height}&cht=qr&chl=$url\" />";
$filename = "images/qr-code/qr-code.png";
$qr = file_get_contents("http://www.test.com/chart?chs={$width}x{$height}&cht=qr&chl=$url");
file_put_contents($filename, $qr);
Please help me to solve this.
I assume that this line:
$url = implode(", ",$datas);
is generating an invalid url. URLs passed as argument to another URL must be url-encoded:
$datas = $_POST['data'];
$width = $height = 100;
$url = urlencode(implode(", ",$datas)); // encode invalid chars
$qr_img = "<img src=\"http://www.test.com/chart?chs={$width}x{$height}&cht=qr&chl=$url\" />";
$qr = file_get_contents("http://www.test.com/chart?chs={$width}x{$height}&cht=qr&chl=$url");
$filename = "images/qr-code/qr-code.png";
file_put_contents($filename, $qr);
see also here: urlencode in PHP
Related
I'm a newbie and I have a url that contains Persian language characters.
For example this:
http://tabnak.ir/fa/news/577155/ویدیوی-درگیری-نیروهای-سیا-و-پنتاگون-در-سوریه-با-همدیگر-ویدیوهایی-از-جنجال-پاسخ-مشایخی-به-مجیدی-و-حرفهای-عجیب-الویس-پریسلی-ایران
When I want to get the html source of that url, with this line of code:
$source = file_get_contents($url);
I get this error:
Warning: file_get_contents(http://tabnak.ir/fa/news/577155/ویدیوی-درگیری-نیروهای-سیا-و-پنتاگون-در-سوریه-با-همدیگر-ویدیوهایی-از-جنجال-پاسخ-مشایخی-به-مجیدی-و-حرفهای-عجیب-الویس-پریسلی-ایران):
failed to open stream: HTTP request failed! HTTP/1.0 400 Bad request in C:\wamp\www\file.php on line 25
I wanted to solve this problem by using the urlencode, but it didn't work.
The urlencode output of that line becomes:
http%3A%2F%2Ftabnak.ir%2Ffa%2Fnews%2F577155%2F%D9%88%DB%8C%D8%AF%DB%8C%D9%88%DB%8C-%D8%AF%D8%B1%DA%AF%DB%8C%D8%B1%DB%8C-%D9%86%DB%8C%D8%B1%D9%88%D9%87%D8%A7%DB%8C-%D8%B3%DB%8C%D8%A7-%D9%88-%D9%BE%D9%86%D8%AA%D8%A7%DA%AF%D9%88%D9%86-%D8%AF%D8%B1-%D8%B3%D9%88%D8%B1%DB%8C%D9%87-%D8%A8%D8%A7-%D9%87%D9%85%D8%AF%DB%8C%DA%AF%D8%B1-%D9%88%DB%8C%D8%AF%DB%8C%D9%88%D9%87%D8%A7%DB%8C%DB%8C-%D8%A7%D8%B2-%D8%AC%D9%86%D8%AC%D8%A7%D9%84-%D9%BE%D8%A7%D8%B3%D8%AE-%D9%85%D8%B4%D8%A7%DB%8C%D8%AE%DB%8C-%D8%A8%D9%87-%D9%85%D8%AC%DB%8C%D8%AF%DB%8C-%D9%88-%D8%AD%D8%B1%D9%81%E2%80%8C%D9%87%D8%A7%DB%8C-%D8%B9%D8%AC%DB%8C%D8%A8-%D8%A7%D9%84%D9%88%DB%8C%D8%B3-%D9%BE%D8%B1%DB%8C%D8%B3%D9%84%DB%8C-%D8%A7%DB%8C%D8%B1%D8%A7%D9%86
Which is not a correct url address, and I can't get contents again.
What should I do?
Can you try this ? This way, you should be able to do the file_get_contents on the encoded url
$url = 'http://tabnak.ir/fa/news/577155/ویدیوی-درگیری-نیروهای-سیا-و-پنتاگون-در-سوریه-با-همدیگر-ویدیوهایی-از-جنجال-پاسخ-مشایخی-به-مجیدی-و-حرفهای-عجیب-الویس-پریسلی-ایران';
$url = mb_convert_encoding($url, 'HTML-ENTITIES', "UTF-8");
$source = file_get_contents($url);
EDIT (TESTED THIS AND WORKS) :
Try this, maybe by encoding the part of the URL where there are arabic character, it might work :
$link = 'http://tabnak.ir/fa/news/577155/ویدیوی-درگیری-نیروهای-سیا-و-پنتاگون-در-سوریه-با-همدیگر-ویدیوهایی-از-جنجال-پاسخ-مشایخی-به-مجیدی-و-حرفهای-عجیب-الویس-پریسلی-ایران';
$exploded = explode('/',$link);
$exploded[6] = urlencode($exploded[6]);
$urlimplode = implode($exploded,'/');
$source = file_get_contents($urlimplode);
echo $source;
In my case, where I wanted to pass a parameter with Get, I did as follows:
$message = "یک پیغام تست";
$link = "http://localhost:5000/myAPI_name?msg=".$message;
$exploded = explode('=',$link);
$exploded[1] = urlencode($exploded[1]);
$urlimplode = implode($exploded,'=');
$source = file_get_contents($urlimplode);
$source=json_decode($source,true);
As the content of $message is Persian, I had to perform encoding just for anything after =
I currentley trying to get data from a website, and use it in my PHP script.
The link I am trying to reach is:
http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=★%20Bayonet
Here is my code:
<?php
$skin = $_GET['market_hash_name'];
$link = "http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=".$skin."";
$FN = file_get_contents($link);
echo $FN;
?>
And this is how I use my link:
http://myhost.com/getPrice.php?market_hash_name=★ Bayonet
This is the error I am getting:
Warning: file_get_contents(http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=★ Bayonet): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in F:\xampp\htdocs\getPrice.php on line 5
EDIT:
Okay, so I have found a solution for my problem, but now a new error is comming up:
Warning: file_get_contents(http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=%E2%98%85+Bayonet+%7C+Stained (Factory New)): failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in F:\xampp\htdocs\getPrice.php on line 7
This is how I am using my link now:
getPrice.php?market_hash_name=★ Bayonet | Stained
And this is my code:
function getPrice($string) {
$skin = urlencode($_GET['market_hash_name']);
$link = "http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=".$skin." ".$string;
$content = file_get_contents($link);
$data = json_decode($content, true);
return $data['lowest_price'];
}
$FN = getPrice("(Factory New)");
$MW = getPrice("(Minimal Wear)");
$FT = getPrice("(Field-Tested)");
$WW = getPrice("(Well-Worn)");
$BS = getPrice("(Battle-Scarred)");
echo "Factory New: $FN; Minimal Wear: $MW; Field-Tested: $FT; Well-Worn: $WW; Battle-Scared: $BS";
Among other characters, non-ASCII characters must be URL-encoded (aka percent-encoded) in query strings.
$skin = url_encode($_GET['market_hash_name']);
$link = "http://steamcommunity.com/market/priceoverview/?country=US¤cy=3&appid=730&market_hash_name=".$skin."";
$FN = file_get_contents($link);
here is my function in a class called getminecraftprofile.php
basically it takes data from https://sessionserver.mojang.com/session/minecraft/profile/
dependant on the UUID of the user and gets an array that i can pick formation from.
the URL strings are base64 encoded so it decodes that as well.
public function getSkin()
{
$properties = $this->properties;
$encoded = json_encode($properties);
$imgUrl = json_decode(base64_decode(json_decode($encoded,true)[0]['value']),true) ['textures']['SKIN']['url'];
$img = file_get_contents($imgUrl);
echo($img);
}
this is the file that is passing in what user profile to find and get the skin of that profile. for most profiles it works perfectly and you can see it in action at http://jamesplanet.net/test/src/getskin.php?username=dinnerbone
<?php
include ($_SERVER["DOCUMENT_ROOT"] . '/test/src/getminecraftprofile.php' )?>
<?php
$profile = ProfileUtils::getProfile("useruser3");
//$result = $profile->getSkin();
$properties = $profile->getProperties();
$encoded = json_encode($properties);
$imgUrl = json_decode(base64_decode(json_decode($encoded,true)[0]['value']),true)['textures']['SKIN']['url'];
$img = file_get_contents($imgUrl);
var_dump($encoded);
echo('Texture URL Decoded: ' . $imgUrl );
it all grinds to a halt however if i say
$profile = ProfileUtils::getProfile("_jeb");
then it throws this at me
Warning: file_get_contents(): Filename cannot be empty in /Applications/AMPPS/www/test/src/getskin.php on line 10
string(908) "[{"name":"textures","value":"eyJ0aW1lc3RhbXAiOjE0MDM0ODE0Mjk2NDMsInByb2ZpbGVJZCI6IjdmM2ZjY2ZjYzUzOTQzNTdhZmEwMjExYTYyODc3MWU0IiwicHJvZmlsZU5hbWUiOiJfamViIiwiaXNQdWJsaWMiOnRydWUsInRleHR1cmVzIjp7fX0=","signature":"xIgM9w3MfZHMjlsZyrMDAF1CTAhhhhSWfjRqJgW3NNTCyotVkL9IMDN6ZzPIoNESFBlhwspxfKhrAC81+GlEUcFxLYilzO6qE+\/pYciNS78b9MxRK7R5xEoNNzFhXAyo9\/cW5X7V6bFxOB2MU9Fg7NXt\/B\/u4VypqGsbCA+OAm7Kgomlhkw3wkm1R1djfd5oBqwlCKhTWxt4k4mQJzThsqE5ffLdTcPeMoipezw4NHAV1QJfXZZlGsEDG\/KkzUDh6eOz+QufYvhcxGrOAfwJ4TzlLbu0QyT8IRIu0E5LY96sTSecWQ27W7TzsumXiXrkpOSLZrOi5yqGFMmfOEHqo8dLRnxhoNmyQpzN2rE4OvNBwKqvzOeRbigOzQSSg5xupydFFLdc20Pj9CXcTB18K12fXt6W8FJ+AzYUccPrSx\/1RJALkef9W5DCxNLOLLR5yIS8Irff3\/Gn2sRwTG8EXJz+6qRB50e02jdyTv9DZz853w+TAVoNtSkBB8W9aC08XUSCFFg1KAaLILkRi8H+76BE1pZKraPAoW8MpsUbd6J+6k0kU5lqdtiJZ1PtDssVMA+p9MwtOmQqD7n4hPXehULBv4+phQjZi37s1I6ZWYrQBE7raq3ZKfDx9ztccmHEyk\/VHSboo0QmtM8iCOCOC1l0Jv9QrV\/YQRZil03x+OQ="}]" Texture URL Decoded:
i know there is data there because its visible before the base64 decode.
where is it going wrong?
here is an example of what is being parsed
{"id":"069a79f444e94726a5befca90e38aaf5","name":"Notch","properties":[{"name":"textures","value":"eyJ0aW1lc3RhbXAiOjE0MDM0ODE0MjIwNDUsInByb2ZpbGVJZCI6IjA2OWE3OWY0NDRlOTQ3MjZhNWJlZmNhOTBlMzhhYWY1IiwicHJvZmlsZU5hbWUiOiJOb3RjaCIsImlzUHVibGljIjp0cnVlLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYTExNmU2OWE4NDVlMjI3ZjdjYTFmZGRlOGMzNTdjOGM4MjFlYmQ0YmE2MTkzODJlYTRhMWY4N2Q0YWU5NCJ9LCJDQVBFIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvM2Y2ODhlMGU2OTliM2Q5ZmU0NDhiNWJiNTBhM2EyODhmOWM1ODk3NjJiM2RhZTgzMDg4NDIxMjJkY2I4MSJ9fX0=","signature":"TJieaNbZrHGmA7V3n8jn0V/ZVPXAy4EK44lxsh3kidK4JML8BEEZZjt/1eixbbXHGMH39e122UO7ehX2PIutuIIK7ZhvAo6wy8yMG34rPxjuyg0jzZe/tZjKv/kq7Bi78m6PErfwBsaUxjkCD3J0sqLXyvOy9rQ2CbYGKTxnVtOZGzxIaQqC5QH/2We704cMUJ3wIgN9FckppVA2E+DYu0G8iFlvYQnjxxIclokv3jOcPrYwVq0COWJaDrGd0+o+dKUfTfiT/k1OLBuceGDmqjb9CmcbEeTFFRJrmA6XEoA6OHZbLOMpIPLtEs8LbZqBH38Cuf+izPGLl1e28bITwPUdpABzvxaxKoYB3XYnWvtAqyfvzsCEgoBVnV7psx1W+kWU9xjCxC591457bollR2KVrSQBv7p0KBm4qVaVPKa01bjcMfcDDx5PRBIcL6XECVmKoeqh+qUrrf1Z9WN0l4OEujmRd8/tbJMjtJTdqCUZ5ak7urJHl8iTNNLqjx/Dsi47DeZvfUGdSC6ivpDIDBQv3myBLNnHqE7/+Z8DGKa+Bf/nQW0sMdVsNFOxIeiXMyXpWAHMw3/Ee21aMWEUq+rD8l4CD2FTZkQ/gv18y3y7CR9fYIKU+EPccnDUtyF+Y257UtCUag7i0vlkiBpIh46jhpjIkhR+iAG4URqQA6I="}]}
So despite hours of fiddling I cannot understand why my JSON query only returns a result for the last line in the CSV/TXT files I am trying to parse.
Here is the code:
//Enter API Key Here
$api_key = 'AIzaSyB9Dq3w1HCxkS5qyELI_pZuTmdK8itOBHo';
$origin = 'RG12 1AA';
$output_type = 'json'; //xml or json
$csv_location = 'http://www.naturedock.co.uk/postcodes.csv';
//Do not edit
$base_url = 'https://maps.googleapis.com/maps/api/directions/';
$origin_url = '?origin=';
$destination_url = '&destination=';
$end_url = '&sensor=false&key=';
$page = join("",file("$csv_location"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
$destination = $kw[$i];
echo $destination;
$raw_url = $base_url . $output_type . $origin_url . $origin . $destination_url . $destination . $end_url . $api_key;
$request_url = str_replace(' ', '', $raw_url);
$getJson = file_get_contents($request_url);
$routes = json_decode($getJson);
$result = $routes->routes[0]->legs[0]->distance->value;
echo $result . '<br>';
}
The result I get looks like this:
Distance by Post Code Generator v0.1 by Phil Hughes
RG12 0GA
RG12 0GB
RG12 0GC
RG12 0GD4066
Where the '4066' is the correct variable for RG12 0GD postcode but none of the others return results as you can see.
Please help.
Your
join("",file("$csv_location"));
concatenated all lines feom the file to a single line without separator. The following explode() sees no newlines any more. So you are working on one line only. count($kw) always evaluates to 1 and your loop runs only one time.
Hi i m trying to get url of user's profile pic so I use following code
$userinfo = $facebook->api("/me/picture?type=large");
var_dump($userinfo);
but instead of returning url it returns NULL. Please help me why this is happening.
Thank you.
This is how we can get actual picture url
$URL='FB GRAPH API URL';
$headers = get_headers($URL, 1); // make link request and wait for redirection
if(isset($headers['Location'])) {
$URL = $headers['Location']; // this gets the new url
}
$url_arr = explode ('/',$URL);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
$pos = strrpos($img_type, "&");
if($pos)
{
$pieces = explode("&", $img_type);
$img_type = $pieces[0];
}
$imagename = imgnameyouwant'.'.$img_type;
$content = file_get_contents($URL);
file_put_contents("fbscrapedimages/$imagename", $content);
If you know the [uid], you can simply use the following code
<img src="http://graph.facebook.com/[UID]/picture" />
You can also crop/resize the profile picture by providing height/width parameters.
Eg: https://graph.facebook.com/[UID]/picture?width=140&height=140
If you want to use standard sizes, try
<img src="http://graph.facebook.com/[UID]/picture?type=large" />
where type is one of {square,small,large}