I desperately want to use an API that asks curl. I do not know anything at all. So I teter to document myself, but I do not understand everything.
Here is what the API says:
All requests must be provided with unique API keys, which you can generate in dashboard. X-API-ID (public) and X-API-KEY (private) parameters. For example:
curl -L http://www.coinimp.com/api/v2/hashes
-H 'X-API-
ID:7e26bb94aa2ce44e6e16aca6ae6d28c7f0157b5ccd7a82f86bbbe8d835effd71'
-H 'X-API-
KEY:5112486af64b2f97bd3742c4153cee32452549491480cfd164b336720b82a84d'
Here is my code:
$curl = curl_init();
$opts = array(
CURLOPT_URL => 'http://www.coinimp.com/api/v2/hashes',
CURLOPT_HEADER => array(
'X-API-
ID:0cd6929b8e34e2cc686eb50bef6a909c4898125b5105221fbfe48a43b038d9ff',
'X-API-
KEY:61dbf2d44abd138bad67c7876dcac0f58b2f08c8bbb91108c7c0984fe7b5f207',
)
);
curl_setopt_array($curl, $opts);
$response = json_decode(curl_exec($curl), true);
print_r($response);
Here is my result:
HTTP/1.1 301 Moved Permanently Date: Sat, 16 Feb 2019 20:19:44 GMT Transfer-Encoding: chunked Connection: keep-alive Cache-Control: max-age=3600 Expires: Sat, 16 Feb 2019 21:19:44 GMT Location: https://www.coinimp.com/api/v2/reward Server: cloudflare CF-RAY: 4aa2b5fd8a25c83d-AMS 1
Please can you help me ?
CoinImp is an absolute mess; I worked with them briefly on a client project and quickly came to dislike their service. You're receiving a 301 response, which indicates that the resource you're requesting is no longer at that address. In the response it appears to give you the new endpoint:
https://www.coinimp.com/api/v2/reward
I would try reformatting your request towards that endpoint. Otherwise, this would be a question for CoinImp support.
I don't know your problem, but i suggest use to use Postman Api Development Environment.
https://www.getpostman.com/
You should enter URL where you will make the request, username, password and additional parameters specified by API provider.
You can test API also you can generate script in different programming languages without writing a single row of code.
Hope this helped you.
Try this:
Replace WEBSITE-ID with the ID you used for miner script.
And replace PUBLIC-ID and SECRET-ID, also USER-ID.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.coinimp.com/api/v2/user/balance?site-key=WEBSITE-ID&user=USER-ID",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"x-api-id: PUBLIC-ID",
"x-api-key: SECRET-ID"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Good luck.
Related
I would like to access IBM Tone Analyzer through PHP using CURL, however, I keep getting authorization errors like shown below. The code I use is shown in the code section.
Although looking up numerous help pages and forums, I haven't found a suitable solution. Could someone please help me how to solve this?
Thanks a lot in advance.
With best regards!
This is the code i use:
<?php
//print_r($_POST);
$ch = curl_init();
$file_path = "tone.json";
$url = "https://api.eu-gb.tone-analyzer.watson.cloud.ibm.com/instances/$BLACKENED$/v3/tone?$
$header_args = array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Basic apikey:$BLACKENED$'
);
curl_setopt_array($ch,array(CURLOPT_URL => $url, CURLOPT_HEADER => $header_args, CURLOPT_UPLOAD => true, CURLOPT_POST => true, CURLOPT_INFILE => $file_path));
$response = curl_exec($ch);
echo($response);
curl_close($ch);
?>
This is the error i get:
HTTP/1.1 401 Unauthorized strict-transport-security: max-age=31536000; includeSubDomains; WWW-Authenticate: Basic realm="IBM Watson Gateway(Log-in)" Content-Length: 164 Content-Type: application/json x-dp-watson-tran-id: $BLACKENED$ x-request-id: $BLACKENED$ x-global-transaction-id: $BLACKENED$ Server: watson-gateway X-EdgeConnect-MidMile-RTT: 0 X-EdgeConnect-Origin-MEX-Latency: 380 Date: Mon, 06 Dec 2021 21:46:28 GMT Connection: close {"code":401,"more_info":"https://cloud.ibm.com/docs/watson?topic=watson-authorization-error","error":"Unauthorized","trace":"$BLACKENED$"} 1
I am using phps file_get_contents to connect with my API server. It works well when the server returns with 200 status (it return the content from the api server). The problem is when the api server returns with 400 status. If the api server returns with 400 status the function simply returns FALSE (not the content).
here is what I get from $http_response_header
[ "HTTP\/1.1 400 Bad Request",
"Server: nginx\/1.16.1",
"Date: Fri, 06 Nov 2020 05:39:09 GMT",
"Content-Type: application\/json",
"Content-Length: 117",
"Connection: close",
"Strict-Transport-Security: max-age=31536000; includeSubDomains; preload",
"Access-Control-Allow-Origin: *",
"Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection" ]
Now my question is how can I get the content sent fron the API server when the status is 400?
You just need to enable the ignore_errors flag:
$context = stream_context_create([
'http' => [
'ignore_errors' => true,
],
]);
var_dump(file_get_contents('http://example.com/', false, $context));
You can rather use curl than file_get_contents as this one has better support for error handling:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "{url}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// handle any other code than 200
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
// your handling code
print_r($output);
}
curl_close($ch);
I am trying to get data from a REST api that uses a token for authentication in the URL. I can pass the relevant parameters in a web browser and get the data with no problem, however as soon as I try to do it in a PHP script with curl, I get a 401 error saying http authentication is required.
Have tried many different options but cannot get it to work, any help would be appreciated. My code is below, have removed site id and changed api_key
API documentation says:
The API can be accessed via HTTPS protocol only. SolarEdge monitoring server supports both HTTPS/1.0 and HTTPS/1.1 protocols.
All APIs are secured via an access token:
Every access to the API requires a valid token as a URL parameter named api_key.
For example: api_key= L4QLVQ1LOKCQX2193VSEICXW61NP6B1O
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLINFO_HEADER_OUT => 1,
CURLOPT_POST => 1,
CURLOPT_HTTPAUTH, CURLAUTH_ANY,
CURLOPT_URL => 'https://monitoringapi.solaredge.com/site/?????/energyDetails',
CURLOPT_USERPWD, 'api_key:6X0kjehfdgksdljsgksdjhfksdhfglksd',
CURLOPT_HTTPHEADER=>array(
"timeUnit:DAY",
"meters=PRODUCTION,CONSUMPTION",
"startTime:2017-12-09 00:00:00",
"endTime:2017-12-09 23:59:59"),
));
$resp = curl_exec($curl);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close request to clear up some resources
curl_close($curl);
echo $resp;
echo "<br>$status_code";
//var_dump ($resp);
//print_r ($resp);
?>
Check the documentation again. Search for (Basic) Authentication.
You should look for something like this:
Authorization: <type> <credentials>
e.g. Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
The HTTP 401 Unauthorized client error status response code indicates that the request has not been applied because it lacks valid authentication credentials for the target resource.
This status is sent with a WWW-Authenticate header that contains information on how to authorize correctly.
This status is similar to 403, but in this case, authentication is possible.
Try your code again like this (add your API key) and report back the results:
$curl = curl_init();
curl_setopt_array($curl, array(
// CURLOPT_PORT=> 443,
// CURLOPT_RETURNTRANSFER => 1,
// CURLOPT_SSL_VERIFYPEER => 0,
// CURLOPT_SSL_VERIFYHOST => 0,
CURLINFO_HEADER_OUT => 1,
CURLOPT_POST => 1,
CURLOPT_HTTPAUTH, CURLAUTH_ANY,
CURLOPT_URL => 'https://monitoringapi.solaredge.com/site/?????/energyDetails',
CURLOPT_USERPWD, 'api_key:6X0kjehfdgksdljsgksdjhfksdhfglksd',
CURLOPT_HTTPHEADER=>array(
"timeUnit:DAY",
"meters=PRODUCTION,CONSUMPTION",
"startTime:2017-12-09 00:00:00",
"endTime:2017-12-09 23:59:59"),
));
$resp = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo $resp;
echo "<br>$status_code";
I am using php and I want to create a HTTP request to access some API data. I have a document that says, I need to place the following request
GET /abc/api/Payment HTTP/1.1
Content-Type: application/x-www-form-urlencoded
X-PSK: [App Key]
X-Stamp: [UTC Timestamp]
X-Signature: [HMACSHA256 base 64 string]
Body:
var1, var1
I have app key, I can get UTC Timestamp and I can create signature. I am not sure how to start creating this request? I am using codeingiter. If someone can help with example to set the header and body?
I also tried this url https://www.hurl.it/ to place requests but can't make it work. Any suggestions?
You want to use cURL's CURLOPT_HTTPHEADER option.
http://php.net/manual/en/function.curl-setopt.php
This should get you started
function request($url) {
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"X-PSK: [App Key]",
"X-Stamp: [UTC Timestamp]",
"X-Signature: [HMACSHA256 base 64 string]"
),
CURLOPT_FOLLOWLOCATION => true
);
curl_setopt_array($ch, $curlOpts);
$answer = curl_exec($ch);
// If there was an error, show it
if (curl_error($ch)) die(curl_error($ch));
curl_close($ch);
return $answer;
}
My code is using file_get_contents() to make GET requests to an API endpoint. It looks like it is using HTTP/1.0 and my sysadmin says I need to use HTTP/1.1. How can I make an HTTP/1.1 request? Do I need to use curl or is there a better/easier way?
Update
I decided to use cURL since I am using PHP 5.1.6. I ended up forcing HTTP/1.1 by doing this:
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
If I was using 5.3 or later I would have tried doing something like this:
$ctx = stream_context_create(array(
'http' => array('timeout' => 5, 'protocol_version' => 1.1)
));
$res = file_get_contents($url, 0, $ctx);
echo $res;
http://us.php.net/manual/en/context.http.php
Note: PHP prior to 5.3.0 does not
implement chunked transfer decoding.
If this value is set to 1.1 it is your
responsibility to be 1.1 compliant.
Another option I found which might provide HTTP/1.1 is to use the HTTP extension
I'd use cURL in either case, it gives you more control and in particular it gives you the timeout option. That's very important when calling an external API so as not to allow your application to freeze whenever a remote API is down.
Could like this:
# Connect to the Web API using cURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.url.com/api.php?123=456');
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xmlstr = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
cURL will use HTTP/1.1 per default, unless you specify something else using curl_setopt($s,CURLOPT_HTTPHEADER,$headers);, where $headers is an array.
Just so others who want to use stream_context_create/file_get_contents know, if your server is configured to use keep-alive connections, the response will not return anything, you need to add 'protocol_version' => 1.1 as well as 'header' => 'Connection: close'. Example below:
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 5,
'protocol_version' => 1.1,
'header' => 'Connection: close'
)
));
$res = file_get_contents($url, 0, $ctx);