How to use file_get_contents() with non-ASCII characters in URL? - php

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&currency=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&currency=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&currency=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&currency=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&currency=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&currency=3&appid=730&market_hash_name=".$skin."";
$FN = file_get_contents($link);

Related

Problems with PHP file_get_contents

My code:
while ($row = $stmt -> fetch(PDO::FETCH_ASSOC)) {
$uri = 'https://www.themoviedb.org/movie/' . $row['tmdb_id'];
$get = file_get_contents($uri);
$pos1 = strpos($get, '<span class="genres">');
$pos2 = strpos($get, '</span>', $pos1);
$text = substr($get,$pos1,$pos2-$pos1+7);
if (strstr ($text, 'Animation'))
array_push ($array, $row['poster_path']);
}
it return me this error:
failed to open stream: HTTP request failed! HTTP/1.1 429 Too Many Requests
in "path/index.php" on line 128
line 128 is: $get = file_get_contents($uri);
Your target url is preventing this from happening. You're basically sending 70 requests in less than one second (probably). This is identified as an attack and at some point blocked.
You need to add some kind of timer to build some kind of queue that does this from time to time, instead of doing it all at once.

Http 400 bad request - Php file_get_contents in Persian URL

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 =

error in file_get_content() in php

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

Invalid argument supplied for foreach() - can't loop through two arrays

My code should convert "defindex" from array_inv into "item_name" from array_schema:
<?php
$apikey = "X";
$steamid = $steamprofile['steamid'];
$url_inv = "http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=". $apikey . "&steamid=" . $steamid . "&format=json";
$url_schema = "http://git.optf2.com/schema-tracking/plain/Counter%20Strike%20Global%20Offensive%20Schema?h=counterstrikeglobaloffensive";
$array_inv_raw = file_get_contents($url_inv);
$array_schema_raw = file_get_contents($url_schema);
$array_inv = json_decode($array_inv_raw,true);
$array_schema = json_decode($array_schema_raw,true);
foreach($array_inv['result']['items'] as $item){
foreach($array_schema['result']['items'] as $schemaItem){
if($item['defindex'] == $schemaItem['defindex']){
echo $schemaItem['item_name'].'<br />';
break;
}
}
}
?>
But it's resulting in these errors:
Warning:
file_get_contents(http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=x&steamid=76561198037897388&format=json):
failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in
/home/kartm/public_html/scripts/inv.php on line 6
Warning: Invalid argument supplied for foreach() in
/home/kartm/public_html/scripts/inv.php on line 12
The urls:
array_inv
array_schema
I can't find any mistakes. Can you tell me what's wrong with this code?
These errors were caused by a typo in a url:
http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=x&steamid=76561198037897388&format=json
It should have been & key=x instead of ?key=x.
file_get_contents couldn't fetch the url, thus the foreach had an invalid, empty argument.

Failed to load url in Dom Document?

I'm trying to get latitude and longtitude from google map by giving address. But it's giving error. When I directly copy url to browser url bar. it's giving correct result. If anybody know the answer.
Here is my code.
<?php
$key = '[AIzaSyAoPgQlfKsBKQcBGB01cl8KmiPee3SmpU0]';
$opt = array (
'address' => urlencode('Kolkata,India, ON') ,
'output' => 'xml'
);
$url = 'http://maps.google.com/maps/geo?q='.$opt['address'].'&output='.$opt['output'].'&oe=utf8&key='.$key;
$dom = new DOMDocument();
$dom->load($url);
$xpath = new DomXPath($dom);
$xpath->registerNamespace('ge', 'http://earth.google.com/kml/2.0');
$statusCode = $xpath->query('//ge:Status/ge:code');
if ($statusCode->item(0)->nodeValue == '200') {
$pointStr = $xpath->query('//ge:coordinates');
$point = explode(",", $pointStr->item(0)->nodeValue);
$lat = $point[1];
$lon = $point[0];
echo '<pre>';
echo 'Lat: '.$lat.', Lon: '.$lon;
echo '</pre>';
}
?>
Following error has occured:
Warning: DOMDocument::load(http://maps.google.com/maps/geo? q=Kolkata%252CIndia%252C%2BON&output=xml&oe=utf8&key=%5BAIzaSyAoPgQlfKsBKQcBGB01cl8KmiPee3SmpU0%5D) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\xampp\htdocs\draw\draw.php on line 19
Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://maps.google.com/maps/geo?q=Kolkata%252CIndia%252C%2BON&output=xml&oe=utf8&key=%5BAIzaSyAoPgQlfKsBKQcBGB01cl8KmiPee3SmpU0%5D" in C:\xampp\htdocs\draw\draw.php on line 19
Notice: Trying to get property of non-object in C:\xampp\htdocs\draw\draw.php on line 26
DOMDocument::load() requires allow_url_fopen to be true.
Check your php.ini-settings and switch it to 1.
See also:
http://php.net/filesystem.configuration.php#ini.allow-url-fopen
What would cause DOMDocument.load to fail loading XML from a URL that is accessible?

Categories