Cannot remote upload through openload API [PHP - curl] - php

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.

Related

PHP Curl Authentication Issue using API

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";

post curl into any file from my server get code 400

I got a new server. I wanted to check if the curl works so I've made 2 files - one that does print_r($_SERVER), and the other that does curl to the first file:
<?php
error_reporting('E_ALL');
$url = $_SERVER['HTTP_HOST'] . '/printServer.php';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => 1 ));
$content = curl_exec($curl);
print $content;
?>
and the outcome is
Bad Request Your browser sent a request that this server could not
understand. Additionally, a 400 Bad Request error was encountered
while trying to use an ErrorDocument to handle the request.
When I change this curl to be get - it does work...
Can anyone help me?
Thanks.

PHP Curl doesn't return a response

I'm trying to get the response back from an API using curl in php but for some reason it fails. However, when I open the API call link in a browser it works fine. Furthermore, if I try to get the response with curl after I've opened the link in the browser curl works but if I change the $url variable to a new link it again stops working until I open it in the browser again.
Here is my code, don't worry about the api key it's just a test:
<?php
set_time_limit(30);
$API_KEY = "ak_wujpBbfefrmxDleyAmnqtFpqAcmey";
$url = 'https://www.google.com/';
$api_call = "https://www.screenshot-website.com/api/$API_KEY?url=$url&type=tablet";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $api_call,
CURLOPT_TIMEOUT => 30,
CURLOPT_TIMEOUT_MS => 30000,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT_MS => 30000 )
);
$response = json_decode(curl_exec($curl), true);
if($errno = curl_errno($curl)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
curl_close($curl); ?>
<img src="<?php echo $response['image']; ?>">
I get no errors at all.
I think your problem is with the "https" since curl set ssl_verifier to true
"The problem is that cURL has not been configured to trust the server’s HTTPS certificate." you can read the full information here.
You could try setting this option:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

PHP get_headers not working?

I want to get headers of website but get_headers return nothing
This is my code
<?php
$url = 'http://www.example.com';
print_r(get_headers($url));
?>
For your information my web hosting provider is network solution
Does the problem from my code or from the web hosting provider ?And what's the solution to get the headers of one website ?
If get_headers is disabled then you can also use cURL instead.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_NOBODY => true));
$header = explode("\n", curl_exec($curl));
curl_close($curl);
print_r($header);

Setting Request Cookies in Redirect saved in cURL CookieJar File

I'm working on a project where I am using an api to interface with an ecommerce store on a separate site. I am able to add items to a basket through the api, and use cURLS CookieJar to save state. To checkout, I want to simply want to link to the actual checkout process on the main e-commerce site. I need to be able to get the cookies stored in the CookieJar to be sent along with the redirect request however.
I attempted to use cURL to grab the cookie and then follow a redirect, but I misunderstood how it works. Instead of redirecting the browser, it issues a new cURL request based on the 302 redirect.
$curlopt = array(
CURLOPT_COOKIEJAR => xxx, // this is the path to the cookie file
CURLOPT_COOKIEFILE => xxx, // this is the path to the cookie file
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_URL => 'http://mydomain.com/redirect_to_ecommerce_checkout.php'
);
$ch = curl_init(); // run curl
curl_setopt_array($ch, $curlopt);
curl_exec($ch);
curl_close($ch);
This does seem to send the correct cookies to the ecommerce page, the problem is the browser doesn't redirect, instead it renders out the HTML from the main ecommerce site on my domain.
How can I set the request cookies and actually perform a redirect?
Thanks in advance.
I think you just need to tell CURL to give you the headers and not to follow the redirect. Then, you can parse for the "Location:" header. Something like this:
$curlopt = array(
CURLOPT_COOKIEJAR => xxx, // this is the path to the cookie file
CURLOPT_COOKIEFILE => xxx, // this is the path to the cookie file
CURLOPT_FOLLOWLOCATION => false, // <--- change to false so curl does not follow redirect
CURLOPT_AUTOREFERER => true,
CURLOPT_URL => 'http://mydomain.com/redirect_to_ecommerce_checkout.php',
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true // Return transfer string instead of outputting directly
);
$ch = curl_init($url);
curl_setopt_array($ch, $curlopt);
$response = curl_exec($ch); // save response to a variable
curl_close($ch);
// try to find Location header in $response
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
if (!empty($matches[1])) {
header("Location: " . trim($matches[1][0]) );
exit;
}
else {
echo 'No redirect found';
}
Note I set CURLOPT_FOLLOWLOCATION => false, CURLOPT_HEADER => true, and CURLOPT_RETURNTRANSFER => true and then inspected the curl_exec() response that I saved in $response.
You could also try:
header("Location: " . curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

Categories