Calling GitHub API route like this
$url='https://api.github.com/search/repositories?q=2021-3-1&sort=stars&order=desc';
$content = file_get_contents($url);
but when I try var_dump($content);die(); th result in browser
bool(false)
That's because GitHub expects you to set a User-Agent header, so you need to create a proper context for file_get_contents:
$url = 'https://api.github.com/search/repositories?q=2021-3-1&sort=stars&order=desc';
$opts = [
'http' => [
'method' => 'GET',
'header' => 'User-Agent: MyAgent/1.0',
]
];
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
Related
<?php
use function PHPSTORM_META\type;
$api = 'api.com';
$token = 'abcd-abcd-abcd';
$options = [
'http' => [
'method' => 'GET',
'header' => "Token: $token"
]
];
$context = stream_context_create($options);
$resp = file_get_contents($api, false, $context);
$resp = json_decode($resp, true);
?>
The above stuff is getting commented out as HTML comments and rest as string. So can you please say how to solve this issue. I searched in online but didn't get the exact answer.
WordPress version is 5.9.4
No Plugins
<!--?php
use function PHPSTORM_META\type;
$api = 'api.com';
$token = 'abcd-abcd-abcd';
$options = [
'http' =--> "['method' => 'GET','header' => "Token: $token"]];$context =stream_context_create($options);$resp = file_get_contents($api, false, $context);$resp =json_decode($resp, true);?>"
Sorry for the messy code in advance. I want to write a code which returns me infos from the official Blizzard API, which I can then print out on my homepage. The code doesn't throw any errors but it doesn't print out something either. For Starters:
I would also prefer using CURL, but my homepage is on a Wordpress Hosting Site and I don't know how to install the CURL Library that way
allow_furl_open is on
$url = "https://eu.battle.net/oauth/token";
$data = array('grant_type' => 'client_credentials');
//HTTP options
$opts = array('http' =>
array(
'method' => 'POST',
'header' => array ('Content-type: multipart/form-data', 'Authorization: Basic ' .
base64_encode("$client_id:$client_pass")),
'content' => json_encode($data)
)
);
//Do request
$context = stream_context_create($opts);
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
$accessToken = $json['access_token'];
$tokenURL = "https://us.api.blizzard.com/data/wow/token/?namespace=dynamic-eu";
$opts2 = array('http' =>
array(
'method' => 'POST',
'header' => array('Content-type: multipart/form-data', 'Authorization: Bearer ' . $accessToken),
)
);
$context2 = stream_context_create($opts2);
$json2 = file_get_contents($tokenURL,false,$context2);
$result2 = json_decode($json2, true);
$tokenprice = $result2['price'];
echo "<p>Tokenpreis:" .$tokenprice. "</p>";
I didn't add the $client_id and $client_pass into the code snippet, but this exists obviously. I used this PHP CURL Snippet as template. And this is a short explanation on blizzard's site on how is this supposed to work:
Anyone got any ideas what went wrong? I am really out of ideas here and would love anyone who could help.
Thanks in advance
Based on the curl example from the linked API docs, the content type should be application/x-www-form-urlencoded, and so for the initial request to https://eu.battle.net/oauth/token you should be able to follow the example here.
In your case this will look something like:
$url = 'https://eu.battle.net/oauth/token';
$data = array('grant_type' => 'client_credentials');
$opts = array(
'http' => array(
'method' => 'POST',
'header' => array (
'Content-type: application/x-www-form-urlencoded',
'Authorization: Basic ' . base64_encode("$client_id:$client_pass")
),
'content' => http_build_query($data)
)
);
$context = stream_context_create($opts);
$json = file_get_contents($url, false, $context);
Also, in your code you are storing the raw result from the initial request in $json, then the decoded array in $result, but attempting to get the access_token from the initial request, instead of the decoded array.
I try to get Dropbox access token using PHP,
https://www.dropbox.com/developers-v1/core/docs#request-token
I made 2 php files,
auth.php:
$Header = array(
"Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"XXX\", oauth_signature=\"XXX&\"\r\n"
);
$Options = array('http' =>
array(
'method' => 'POST',
'header' => $Header,
)
);
$Context = stream_context_create($Options);
$Result = file_get_contents("https://api.dropbox.com/1/oauth/request_token", false, $Context);
echo '<a target="_blank" href="https://www.dropbox.com/1/oauth/authorize?'.$Result.'&oauth_callback=http://localhost/dropbox/access_token.php">auth</a>';
auth.php file is working good! and redirect me to dropbox site to accept me app
but when redirect to access_token.php file, i can't get my access token!
I got error: failed to open stream: HTTP request failed! HTTP/1.1
access_token.php file:
$Options = array('http' =>
array(
'method' => 'POST',
)
);
$au = $_GET['oauth_token'];
$Context = stream_context_create($Options);
$Result = file_get_contents("https://api.dropboxapi.com/1/oauth/access_token?oauth_token=$au", false, $Context);
print_r($Result);
Check selinux setting for httpd_network_connect in Apache. It seems that file get content fails to open the stream.
I have what I think is correctly written code yet whenever I try and call it I'm getting permission denied from Google.
file_get_contents(https://www.googleapis.com/urlshortener/v1/url): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
This isn't a rate limit or anything as I currently have zero ever used...
I would have thought this is due to an incorrect API key but I've tried resetting it a number of times. There isn't some downtime while the API is first applied is there?
Or am I missing a header setting or something else just as small?
public function getShortUrl()
{
$longUrl = "http://example.com/";
$apiKey = "MY REAL KEY IS HERE";
$opts = array(
'http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json",
'content' => json_encode(array(
'longUrl' => $longUrl,
'key' => $apiKey
))
)
);
$context = stream_context_create($opts);
$result = file_get_contents("https://www.googleapis.com/urlshortener/v1/url", false, $context);
//decode the returned JSON object
return json_decode($result, true);
}
It seems I need to manually specify the key in the URL
$result = file_get_contents("https://www.googleapis.com/urlshortener/v1/url?key=" . $apiKey, false, $context);
This now works. There must be something funny with how the API inspects POST for the key (or lack of doing so).
Edit: For anyone in the future this is my complete function
public static function getShortUrl($link = "http://example.com")
{
define("API_BASE_URL", "https://www.googleapis.com/urlshortener/v1/url?");
define("API_KEY", "PUT YOUR KEY HERE");
// Used for file_get_contents
$fileOpts = array(
'key' => API_KEY,
'fields' => 'id' // We want ONLY the short URL
);
// Used for stream_context_create
$streamOpts = array(
'http' =>
array(
'method' => 'POST',
'header' => [
"Content-type: application/json",
],
'content' => json_encode(array(
'longUrl' => $link,
))
)
);
$context = stream_context_create($streamOpts);
$result = file_get_contents(API_BASE_URL . http_build_query($fileOpts), false, $context);
return json_decode($result, false)->id;
}
I am currently using the below mentioned code to make http post request and it only returns body. I want the body and headers both. How I can I get body and headers both with file_get_content method or CURL?
$sURL = "url";
$sPD = "data";
$aHTTP = array(
'http' =>
array(
'method' => 'POST',
'header' => "Content-Type: application/atom+xml"
'content' => $sPD
)
);
$context = stream_context_create($aHTTP);
$contents = file_get_contents($sURL, false, $context);
echo $contents;
Try:
$context = stream_context_create($aHTTP);
$stream = fopen($sURL, 'r', false, $context);
$contents = stream_get_contents($stream);
$metadata = stream_get_meta_data($stream);
You can also use the HTTP Functions if available.