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.
Related
Is it possible to process woocommrce api using curl?
I am trying to do it but no success. This api works in insomnia or postman
To Process
curl https://example.com/wp-json/wc/v3/products -u consumer_key:consumer_secret
Following is what I am doing
https://www.example.com/wp-json/wc/v3/products
$Consumer_Key="ck_111111";
$Consumer_Secret= "cs_222222";
$options = array(
CURLOPT_URL => $URL,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_USERPWD => $Consumer_Key.":".$Consumer_Secret
);
$ch=curl_init();
curl_setopt_array($ch, $options);
// Execute request, store response and HTTP response code
$response=curl_exec($ch);
curl_close($ch);
print_r($response);
And The error I am getting is
Forbidden
You don't have permission to access this resource.
Additionally, a 403 Forbidden
error was encountered while trying to use an ErrorDocument to handle the request.
I don't understand a lot of PHP but I have the same code working well on Angular with the cURL:
getCategories() {
this.http.get(this.cUrl + "/wp-json/wc/v3/products/categories?per_page=100&consumer_key=" + this.wooApiClie + "&consumer_secret=" + this.wooApiSec).subscribe(res => {
this.categories = res;
console.log(this.categories);
})
This one is for the categories but it's the same for the products. Maybe it's because your variables $Consumer_key and $Consumer_secret are in caps? I repeat, I don't understand too much of PHP.
I have made the same request in Postman and it gives me the correct response, but when i make the same call from my website it crashes.
Im simply trying to make a GET request from PHP with one header, the oauth2 authorization token.
Code (googleLogin.php):
<?php
$url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Authorization: Bearer " . $_GET["access_token"] . "\r\n"
)
);
$context = stream_context_create($options);
$file = file_get_contents($url, false, $context);
print_r($file);
?>
Error:
PHP Warning:
file_get_contents(https://www.googleapis.com/oauth2/v1/userinfo?alt=json):
failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
in /home/shawn/public_html/cloud/php/googleLogin.php on line 14
I dont know if this error implies (i) The file googleLogin.php on the web server doesnt have correct permissions (i tried 777), (ii) The access token is expired/invalid (it cant be because i tried the same token 2 seconds later successfully with Postman), or (iii) php is formatting my HTTP request incorrectly
I am using PHP to get JSON from a remote server via file_get_contents command. Here is the piece of code I used:
$opts = array(
'https'=>array(
'method'=>'GET',
'header'=>'Accept-language: en\r\n' .
'Authorization: MAC ["3","ios2.5.0","123","123abc","123=","abc="]\r\n' .
'User-Agent: abc/1.1.1 iOS/10.0.2 iPhone/iPhone7,1\r\n'
)
);
$context = stream_context_create($opts);
$file = file_get_contents('https://www.google.com/v11/file?search=ios&with=users%2Cfiles%2Cquestions', false, $context);
echo $file;
I did a quick debugging:
Using Postman I was able to get the json file with the same header.
I tried a different json from a different url, it works.
I tried a local file, it works.
You have to understand what file_get_contents is. This command is a request to get the file on the server, in this case it is requesting to get https://www.google.com/v11/file/index.html on the server as in one single step. Since your url seems to use header to verify your origin, it might be an ajax request, meaning the server components didn't set up to allow an output from file_get_contents requests, instead they probably accept cURL requests.
So you can use:
curl_exec()
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)
I'm having some trouble POSTing data from a client machine to our internal site using PHP. The server accepts data via HTTPS with basic authentication. Here is the code I'm using to POST with:
$parameters = array('http' => array(
'method' => 'POST',
'content' => $data
)
);
if ($optionalHeaders !== NULL) {
$parameters['http']['header'] = $optionalHeaders;
}
$ctx = stream_context_create($parameters);
$fp = fopen($url, 'rb', false, $ctx);
With the following header:
$postHeader = "POST /index.php HTTP/1.1\r\n".
"Host:my.host\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"User-Agent: PHP-Code\r\n".
"Content-Length: " . strlen($postData) . "\r\n".
"Authorization: Basic ".base64_encode($user.':'.$password)."\r\n".
"Connection: close\r\n";
Now, I can get this to work, and it posts just fine on one of my clients with PHP version 5.2.5, but the on another client I get this error message:
fopen(magical_url): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
And Apache error log gives:
request failed: error reading the headers
The only difference I can see is that the latter client has PHP version 5.1.6.
Does anyone know if this is a bug? Or am I doing something wrong somewhere...
I've looked through the PHP site and found this bug listed for version 5.2.6 of PHP https://bugs.php.net/bug.php?id=45540 but this post-dates the version it works on!
Thanks,
Jak
You should not provide all those headers. That's bound to fail.
Headers like POST, Host, Content-Length and Connection are automatically provided. Remove all your optional headers (should work then) and add them step by step.