I'm relatively new to programming in all aspects, so please bear with me when asking stupid questions. I'd like to get the steam price histories of selected items through Steam's market API, which is easily accessible through my browser when I'm logged in Steam. Thing is, I'd like to do it through a server, which gets me the following error:
Warning: file_get_contents(http://steamcommunity.com/market/pricehistory/?currency=1&appid=440&market_hash_name=Specialized%20Killstreak%20Brass%20Beast): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in PATH on line 16
Basically, my problem is the same as this guy's: How to retrieve steam market price history?, but his answers are for Python, and I really don't know anything about the Python language.
If would like to do this in PHP, or if it's not possible in PHP I could use JavaScript.
I'm not asking for a code I can copy-paste, I'd be quite happy with anything to start with.
The code that I am trying to get to work:
// $page is the URL above
$json = json_decode(file_get_contents($page), true);
if($json["success"] == true OR !empty($json))
{
$results = $json["prices"];
foreach ($results as $result)
{
echo $result . "<br>";
}
The answer you linked to sets the steamLogin cookie to enable access to the Steam API. If you grab that from your browser's cookies, then you can add it to your file_get_contents request is as follows:
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=> "Cookie: steamLogin=76561198058933558%7C%7C2553658936E891AAD\r\n"
)
);
$context = stream_context_create($opts);
// Call the API using the HTTP headers set above
$file = file_get_contents('http://steamcommunity.com/market/pricehistory/?currency=1&appid=440&market_hash_name=Specialized%20Killstreak%20Brass%20Beast', false, $context);
(reference question)
Related
I'm trying to use the discogs php api in a PHP script to get info on a release using the release ID. For example, when I make a request to:
http://mywebsite.com/test.php?id=1017868
I want to call the discogs API to get info on the release with id = 1017868. I can see the info I want by manually going to:
https://api.discogs.com/releases/1017868
So I have my $consumerKey =and $consumerSecret and I'm following the DISCOGS AUTH FLOW instructions, which says I can send my keys in a get request like so:
curl "https://api.discogs.com/database/search?q=Nirvana" -H "Authorization: Discogs key=foo123, secret=bar456"
I'm trying to make a get request for my target id in my php script like so:
<?php
echo "hello world <br>";
//discogs simple auth flow, http requests
$remote_url = 'https://api.discogs.com/releases/1017868';
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header' => "Authorization: Discogs key=mykeyasdlkhaskld, secret=mysecretkeykjnasdkjnsadkj"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents($remote_url, false, $context);
print($file);
echo "end of program";
?>
But I keep getting the error:
"failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden".
Is there something I'm forgetting for making my request to the server? Thanks.
I am working on a webpage where I have to get data from an API (with PHP). The authenatication works fine and the user can log in. To save the access token I use the function setcookie() in php. However after some time the data dissapears and I get the following warning:
Warning: file_get_contents(https://...#me): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in C:\wamp\www\main.php on line 40
These are the lines:
function getUser($access_token){
$url = "https://jawbone.com/nudge/api/v.1.0/users/#me";
$opts = (array(
'http'=>array(
'method'=>"GET",
'header'=>"Authorization: Bearer {$access_token}\r\n"
)
));
$context = stream_context_create($opts);
$response = file_get_contents($url, false, $context); //
$user = json_decode($response, true);
return $user['data'];
}
It's quite weird actually because it works when I delete the access token cookie and then log in (and authenticate) again... I simply do not understand why this is happening.
Setting the cookie (expires in is 31536000):
if (!isset($_COOKIE['access_token'])) {
setcookie('access_token', $data, time() + ($json['expires_in']));
}
Can you please tell me what I am doing wrong?
Check your expiry date of the cookie, even though the token may be valid on your side, the cookie may expire earlier.
Maybe break it up a bit. Start at the simplest task, make it work. So, let's say you need an api call, make that call work. When that works, move on to the next step.
If the api call needs a secret token, I can imagine you don't want to just give it to any user to store it as a cookie.
Next step would be authentication. If you're not only new to PHP, but web applications in general, I would start with something less challenging.
But let's assume you'll master it quite soon, the next step (there is never a last step) could be a security decision: does the user need to do the api call, or can I do that for the user by doing the api call server side once the user is authorized?
page: http://www.nastygal.com/accessories/minnie-bow-clutch
code: $html = file_get_contents('http://www.nastygal.com/accessories/minnie-bow-clutch');
The $html always contains the USD price of the product even when I change the currency on the upper right of the page. How do I capture the html that has the CAD price when I change the currency of the page to CAD?
It looks like currency preferences are being saved in a cookie named: CURRENCYPREFERENCE
Since it's not your browser making the connection to retrieve that view, you're likely not sending any cookie data along with your request.
I believe example #4 here will get you what you need:
http://php.net/manual/en/function.file-get-contents.php
It seems as though the country and currency selection are stored in cookies.
I'm assuming you're going to have to pass those values along with your file_get_contents() call. See: PHP - Send cookie with file_get_contents
EDIT #1
To follow up on my comment, I just tested this:
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: CURRENCYPREFERENCE=cad\r\n"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.nastygal.com/accessories/minnie-bow-clutch', false, $context);
print_r($file);
And was able to get this:
EDIT #2:
In response to your second comment. Those were important details. What does your bookmarklet do with the scraped contents? Are you saving a copy of the bookmarked product page on your own website? Regardless, you're going to have to modify your bookmarklet to check the user's cookies before submitting the request to run file_get_contents().
I was able to access my cookies from nastygal.com using the following simple bookmarklet example. Note: nastygal.com uses jQuery and the jQuery UI cookie plugin. If you're looking for a more generic solution, you should not rely on these scripts being there:
javascript:(function(){ console.log($.cookie('CURRENCYPREFERENCE')); }());
Output in the JS console:
cad
I am trying to scrape a suppliers magento site in an effort to save some time because of there being around 2000 products I need to gather info for. I'm totally OK with writing a screen scraper for pretty much anything but i've encountered a major problem. Im using get_file_contentsto gather the html of the product page.
The problem is:
You need to be logged in, to view the product page. Its a standard magento login, so how can I get round this in my screen scraper? I don't require a full script, just advice on a method.
Using stream_context_create you can specify headers to be sent when calling your file_get_contents.
What I'd suggest is, open your browser and login to the site. Open up Firebug (or your favorite Cookie viewer) and grab the cookies and send them with your request.
Edit: Here's an example from PHP.net:
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
?>
Edit (2): This is out of the scope of your question, but if you are wondering how to scrape the website afterwards you could look into the DOMDocument::loadHTML method. This will essentially give you the required functions (i.e. XPath query, getElementsByTagName, getElementsById) to scrape what you need.
If you want to scrape something simple, you can also use RegEx with preg_match_all.
If you're familiar with CURL this should be relatively simple to do in a day or so. I've created some similar apps to login to banks to retrieve data - which of course also require authentication.
Below is a link with an example of how to use CURL with cookies for authentication purposes:
http://coderscult.com/php/php-curl/2008/05/20/php-curl-cookies-example/
If you can grab the output of the page you can parse for your results with a regex. Alternatively, you can use a class like Snoopy to do this work for you:
http://sourceforge.net/projects/snoopy/
I am using PHP with the Amazon Payments web service. I'm having problems with some of my requests. Amazon is returning an error as it should, however the way it goes about it is giving me problems.
Amazon returns XML data with a message about the error, but it also throws an HTTP 400 (or even 404 sometimes). This makes file_get_contents() throw an error right away and I have no way to get the content. I've tried using cURL also, but never got it to give me back a response.
I really need a way to get the XML returned regardless of HTTP status code. It has an important "message" element that gives me clues as to why my billing requests are failing.
Does anyone have a cURL example or otherwise that will allow me to do this? All my requests currently use file_get_contents() but I am not opposed to changing them. Everyone else seems to think cURL is the "right" way.
You have to define custom stream context (3rd argument of function file_get_contents) with ignore_errors option on.
As a follow-up to DoubleThink's post, here is a working example:
$url = 'http://whatever.com';
//Set stream options
$opts = array(
'http' => array('ignore_errors' => true)
);
//Create the stream context
$context = stream_context_create($opts);
//Open the file using the defined context
$file = file_get_contents($url, false, $context);