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";
Related
I'm trying to upload a file using the openload API that can be found here.
When I upload some file I always get a JSON response of success (code: 200):
Unfortunately when I check the file status (using both API and website) it tells me that there was an error in upload. Here the JSON response (code 200):
Here is how I send the request. Where's the error? I tried using both the urlencode function and not.
$user = "...";
$psw = "...";
$link = urlencode("https://google.com/favicon.ico");
$url = "https://api.openload.co/1/remotedl/add?login=$user&key=$psw&url=$link";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
$resp = curl_exec($curl);
curl_close($curl);
echo $resp;
EDIT: Switched to https and added CURLOPT_FOLLOWLOCATION => true, still no success.
I think you are hitting a http 301 redirect while using curl
http://google.com/favicon.ico
to
https://www.google.com/favicon.ico
If that case curl option CURLOPT_FOLLOWLOCATION might help.
I'm generating a URL such as:
http://maps.googleapis.com/maps/api/geocode/json?address=143+Blackstone+Street%2C+Mendon%2C+MA+-+01756&key=***********
If I open this in browser, I'm getting a result successfully. However, if I use the same URL using curl, I'm getting:
error:couldn't connect to host
Here is my PHP code
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode('143 Blackstone Street, Mendon, MA - 01756')."&key=***********";
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 100,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => false
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if(curl_error($ch))
{
echo 'error:' . curl_error($ch);
}
curl_close ($ch);
print_r($response);
UPDATE
I came across a term called as server key and browser key when using the Google APIs. Howerver, in the API manager, I can't seem to see where to set or change the API type.
You are urlencodeing parts of the url that need to be in plain text.
Only urlencode your parameters
<?php
$address = urlencode("143 Blackstone Street, Mendon, MA - 01756");
$key = urlencode("************");
$ch = curl_init();
$options = array(
CURLOPT_URL => "http://maps.googleapis.com/maps/api/geocode/json?address=".$address."&key=".$key,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 100,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => false
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if(curl_error($ch))
{
echo 'error:' . curl_error($ch);
}
curl_close ($ch);
print_r($response);
If you're hitting an 'couldn't connect to host' error message from curl it sounds like the problem may not be on the Google Maps API side and instead be on the proxy / networking side. A connection to maps.googleapis.com should always be possible, regardless of key type or url params.
I would look into the steps suggested here: How to resolve cURL Error (7): couldn't connect to host?
I'm sending the following request to Google Analytics:
http://www.google-analytics.com/collect?v=1&tid=UA-72579327-1&cid=61baecac-8f8c-4dce-bf07-a7efa24a4e47&t=transaction&ti=qcY6pvpWGmP9fHyi&tr=10.00&cd1=Acme Racing&cd2=http://www.domain.co.uk/affiliate/idevaffiliate.php/id=314&tid1=12044762460674420322&url=http://www.nitrotek.co.uk&cd3=A1&cd4=Upgrades&cd5=Acme-Tech &cd6={device}&cd7=&cd8=0&cd9=&cd10=&cd11=&cd12=Nitrotek DSA&cd13=g&cd14=50&cd15=1&cd16=85&cd17=12044762460674420322&cd18=&cd19=http://www.domain.co.uk&cd20=61baecac-8f8c-4dce-bf07-a7efa24a4e47&gclid=
When you go to Google's hit builder and validate the request, it comes out valid.
However, when I send the same request through CURL POST, I just get "400. That’s an error. Your client has issued a malformed or illegal request. That’s all we know.".
POST is the correct method for this request (though I have tried GET just in case) and the Content-Length header is the length of the sent data (string). Here is the code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $transaction_url, //the same URL string given above
CURLOPT_POST => 1,
CURLOPT_USERAGENT => 'cURL Request'
));
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Length: '.strlen($transaction_url) //length of URL string
));
$trans_resp = curl_exec($curl);
var_dump($trans_resp);
curl_close($curl);
I am trying to figure out how to use Pin Payments API, function get charge.
https://pin.net.au/docs/api/charges#get-charge
As you can see they provide an example of the request using CURL
curl https://test-api.pin.net.au/1/charges/ch_bZ3RhJnIUZ8HhfvH8CCvfA -u your-secret-api-key:
I am just not sure how do I convert that to PHP, how do I pass my API key and how to pass charge ID so that the URL understood by the server.
Please help me comple the code below:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://test-api.pin.net.au/1/charges/ch_bZ3RhJnIUZ8HhfvH8CCvfA',
));
$resp = curl_exec($curl);
curl_close($curl);
Look up the u command line parameter: http://curl.haxx.se/docs/manpage.html
It represents the username and the password being used for the request.
Look up curl_setopt(). CURLOPT_USERPWD is the option for setting these credentials:
curl_setopt($curl, CURLOPT_USERPWD, 'apikey:');
// or
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://test-api.pin.net.au/1/charges/ch_bZ3RhJnIUZ8HhfvH8CCvfA',
CURLOPT_USERPWD => 'apikey:'
));
Please pay attention to the trailing colon! It separates the username from the password, which is empty in this case.
I'm working with REST in my Zend Framework 2 application. In order to test my rest calls, I use Curl to send REST requests as follows:
$client = new Http\Client();
$client->setAdapter(new Http\Client\Adapter\Curl());
$request = new Http\Request();
$request->setUri($url);
$request->setMethod($method);
$response = $client->dispatch($request);
$jsonString = $response->getContent();
Everthing works fine except that the Curl requests seem to ignore the session. I need access to the logged-in user in my rest request handling to e.g. make Acl work. When I send REST request using AngularJS, the session actually works. Here's how I set up the session:
$storage = new SessionArrayStorage();
$manager = new SessionManager($config, $storage);
$session = new Session('surveylab', $manager);
How can I pass the session information to Curl, so I get access to the session in the Curl requests?
Checkout the cURL options for:
CURLOPT_COOKIEFILE
CURLOPT_COOKIEJAR
look for examples for the cURL usage of them:
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
Send your login credentials as post to your login url, extract the cookies from the result and send them with the next request. I don't know if your application updates the cookies over time but you should update them if necessary.
Some example code:
curl_setopt_array($ch, array(
CURLOPT_URL => "http://your/url",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array('Cookie: ' . $cookies . '"'),
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_POST => count($postData),
CURLOPT_POSTFIELDS => $post_string
));