cURL on proxied localhost returns Access Denied - php

I'm trying to access a .php file from another .php using cURL.
Both are on localhost and I'm using a proxy.
THE PROBLEM
If I try to access using postman or direct on the browser, the desired file returns correctly.
However, everytime that I try to access this file from another .php file using cURL, and passing all the proxy and auth values, it gives me the following:
Access Denied (authentication_failed)
Your credentials could not be authenticated: "Credentials are missing.". You will not be permitted access until your credentials can be verified.
This is typically caused by an incorrect username and/or password, but could also be caused by network problems.
For assistance, contact your network support team.
Her is my cURL:
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_PROXY => "proxy.myspecificproxy.com.br",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => "localhost/MyDesiredFile.php?param=test&mail=test%40test.com",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_PROXYUSERPWD => "myuser:mypassword123",
CURLOPT_PROXYPORT => "8080",
CURLOPT_PROXYTYPE => 'HTTP',
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Any ideas?
EDIT:
Beyond trying to access via postman or browser, if I cURL directly from the command line, it works too.
EDIT
Tried to disable the proxy for local addresses, but no change in the result.

Is there a redirect involved? Remember that cURL doesn't follow redirects unless you tell it to, by passing the --location flag.
Does it work if you run it from the command line? Try that, and try passing --verbose and --include to see the returned headers.

Update your code as below
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_PROXY => "proxy.myspecificproxy.com.br",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => "localhost/MyDesiredFile.php?param=test&mail=test%40test.com",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_PROXYUSERPWD => "myuser:mypassword123",
CURLOPT_PROXYPORT => "8080",
CURLOPT_PROXYTYPE => 'HTTP',
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

<?php
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => "http://localhost/",
CURLOPT_FOLLOWLOCATION => true,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Looks like it was a misconfiguration in the privileges.
I contacted the support team of the company and the problem was solved.

Related

How to get data from a website that uses SSL using cURL and PHP?

I am trying to get some data from a website where you need to have a SSL certificate to be able to connect to it.
I have found the following code :
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $host);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $host);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
The cURL query works well, but the data that I get is this one :
400 No required SSL certificate was
sent 400 Bad
Request No required SSL certificate was
sent nginx
What I am looking for is the data contained in the index.php (and the other paths) of the website.
So, how can I do to add a Certificate to the code, and, using cURL, get the data from the website ?
PS : Will the data will be in JSON format ?
PPS : if this can be helpfull, I am using PHPStorm
Try this.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://jsonplaceholder.typicode.com/todos/1",
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("content-type: application/json"),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
can you try like this?
echo $str = file_get_contents("https://jsonplaceholder.typicode.com/todos/1");

cURL in not working in php

cURL is enabled in the server. But it's showing some error from the curl_exec($curl) point. No other error is showing from curl_error($curl);
Fatal error: Original Handler not found! in /public_html/curl_test.php on line 18
The cURL package is curl-7.29.0-46.el7.x86_64.
Could anyone suggest me any solution or can help me to troubleshoot the issue?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$curl = curl_init();
curl_setopt_array($curl, Array(
CURLOPT_URL => 'http://someurl.com',
CURLOPT_TIMEOUT => 120,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => 'UTF-8'
));
if(curl_exec($curl) === false)
{
echo 'Curl error: ' . curl_error($curl);
}
else
{
$data = curl_exec($curl);
echo 'Operation completed without any errors';
}
curl_close($curl);
The issue was in the zlib.output_compression.
It fixed and curl is working now.

could not decode nested json api php

I am using these code to fetch koinex api data. From this API URL -
https://koinex.in/api/ticker
<?php
$getCurrency = "inr";
$displayArrayOutput = true;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://koinex.in/api/ticker",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
if($displayArrayOutput){
$response = json_decode($response, true);
print_r($response);
}
else{
header("Content-type:application/json");
}
}
?>
I have also try file_get_contents but same problem. I have face this issue in 2 more api's. Note: Once i get the data and use it correctly but today this is not working again.
I tried your code, and apparently the website you are trying to reach with CURL uses a security:
Why have I been blocked?
This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.
What can I do to resolve this?
You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.
It seems that the website you are trying to reach asks for a user agent. This code works for me:
<?php
$getCurrency = "inr";
$displayArrayOutput = true;
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://koinex.in/api/ticker',
CURLOPT_USERAGENT => 'Something here'
));
// Send the request & save response to $resp
$response = curl_exec($curl);
// Close request to clear up some resources
$err = curl_error($curl);
curl_close($curl);
if ($err) {
} else {
if($displayArrayOutput){
$response = json_decode($response, true);
print_r($response);
}
else{
header("Content-type:application/json");
echo 'touine';
}
}
?>
Good luck

Call online-convert rest api

I'm using this service online-convert trying to make a simple call like in the online-convert sample. However they do not have examples of actual code so I'm kinda in the dark. online-convert docs
I have got that far, here is my best guess at what a call should look like:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api2.online-convert.com/jobs");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Host: https://api2.online-convert.com",
"X-Oc-Api-Key: <my api key>",
"Content-Type: application/json"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
Any help will be appreciated.
They are using a REST API to convert files. Here is an example.
First create a json file with a link where the file you want to convert can be downloaded (uploads are handled differently). Also add the format you want to convert to. Save it as test.json.
{
"input": [{
"type": "remote",
"source": "https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png"
}],
"conversion": [{
"category": "image",
"target": "png"
}]
}
Then send this file using curl to the API of online-convert.com. Add your API key to the script below and save it as start.php in the same directory where you saved the test.json:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api2.online-convert.com/jobs",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => file_get_contents('test.json'),
CURLOPT_HTTPHEADER => array(
"content-type: application/json",
"x-oc-api-key: <your API key here>"
),
)
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Execute the PHP file on the command line with
php save.php
You can also call the script using a webbrowser.
After you have successfully sent the job and got a valid response, you can obtain the status of the conversion. For this you need the id (job id) you got in the answer when executing start.php. Create a file called status.php and execute it.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api2.online-convert.com/jobs/<your job id here>",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"content-type: application/json",
"x-oc-api-key: <your API key here>
),
)
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
There you will find the URL to download your file.
The API is much more powerful than that. You can upload files you want to convert, create multiple conversions of one single file (e.g. videos in different resolution with one API call) and set various conversion options.

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

Categories