SDCore
SDCore

Reputation: 41

PHP file_get_contents returns with a 400 Error

My problem is pretty straightforward, but I cannot for the life of me figure out what is wrong. I've done something similar with another API, but this just hates me.

Basically, I'm trying to get information from https://owapi.net/api/v3/u/Xvs-1176/blob and use the JSON result to get basic information on the user. But whenever I try to use file_get_contents, it just returns

Warning: file_get_contents(https://owapi.net/api/v3/u/Xvs-1176/blob): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST in Z:\DevProjects\Client Work\Overwatch Boost\dashboard.php on line

So I don't know what's wrong, exactly. My code can be seen here:

$apiBaseURL = "https://owapi.net/api/v3/u";
$apiUserInfo = $gUsername;
$apiFullURL = $apiBaseURL.'/'.$apiUserInfo.'/blob';

$apiGetFile = file_get_contents($apiFullURL);

Any help would be largely appreciated. Thank you!

Upvotes: 1

Views: 1971

Answers (3)

LF-DevJourney
LF-DevJourney

Reputation: 28529

You need to set user agent for file_get_contents like this, and you can check it with this code. Refer to this for set user agent for file_get_contents.

<?php
$options  = array('http' => array('user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0'));
$context  = stream_context_create($options);
$response = file_get_contents('https://owapi.net/api/v3/u/Xvs-1176/blob', false, $context);
print_r($response);                  

Upvotes: 2

vicatcu
vicatcu

Reputation: 5837

If you do curl -v https://owapi.net/api/v3/u/Xvs-1176/blob you will get a response and you will see what headers cURL includes by default. Namely:

> Host: owapi.net
> User-Agent: curl/7.47.0
> Accept: */*

So then the question is, which one does owapi care about? Well, you can stop cURL from sending the default headers like so:

curl -H "Accept:" -H "User-Agent:" -H "Host:" https://owapi.net/api/v3/u/Xvs-1176/blob

... and you will indeed get a 400 response. Experimentally, here's what you get back if you leave off the "Host" or "User-Agent" headers:

{"_request": {"api_ver": 3, "route": "/api/v3/u/Xvs-1176/blob"}, "error": 400, "msg": "Hi! To prevent abuse of this service, it is required that you customize your user agent."}

You actually don't need the "Accept" header, as it turns out. See the PHP docs on how to send headers along with file_get_contents.

Upvotes: 1

That's what page is sending: "Hi! To prevent abuse of this service, it is required that you customize your user agent". You can customize it using curl like that:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://owapi.net/api/v3/u/Xvs-1176/blob"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 
$output = curl_exec($ch);   

$output = json_decode($output);

if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
  var_dump($output);
}

curl_close($ch);

Upvotes: 1

Related Questions