cURL 401 Unauthorized only on Webserver - localhost working fine - php

I am trying to download files via cURL using a Bearer as authorization.
While this works fine on my localhost, it somehow does not work on my webserver:
$cookie_path = $plugin_tmp_path . 'session.txt';
$ch = curl_init();
$options = array(
CURLOPT_URL => $file,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array(
"Content-Type: image/jpeg",
"Accept: application/json",
"Authorization: Bearer " . $_SESSION['fm-data-api-token'],
"Cookie: fm-data-api-token=" . $_SESSION['fm-data-api-token'],
),
CURLOPT_COOKIEJAR => $cookie_path,
CURLOPT_COOKIEFILE => $cookie_path,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
The cookie is being written, so this should not be the problem.
Any ideas?

Related

How to get WordPress posts count using PHP Curl

I'm using php curl API to fetch the posts from WordPress. But I'm not getting X-WP-Total or X-WP-TotalPages keys with response to work with pagination.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/wp-json/wp/v2/posts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'Accept: application/json',
"Content-Type: application/json",
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: GET, POST, OPTIONS",
"Access-Control-Allow-Headers: X-Requested-With"
],
));
$response = curl_exec($curl);
$response = json_decode($response);
// print_r($response);exit;
curl_close($curl);

Getting a response through Curl URL in PHP

I am trying to perform some function based on the response I will get from Curl URL. Below is the code I have written. But the response coming is always empty. Due to which the control always goes into an else condition.
if(isset($_POST['verify_button'])){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://*******/v1/****/*********/".$_POST['********']."?MVNO=*******",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic Z*******",
"accept: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response);
if($data->resultCode == '0'){
echo '<div class="mm_success_alert">Sucess</div>';
}
else{
echo '<div class="mm_danger_alert">Fail.</div>';
}

PHP cURL request headers isn't working with a global variable

I've set a global variable to equal an access token, and when I print_($session_id) the token is visible. What I'd like to do is pass that variable into my CURLOPT_HTTPHEADER, and have it equal to "Session" in the function get_field_data_id() below:
function get_session_id() {
global $session_id;
$curl = curl_init();
$session_url = "/auth";
curl_setopt_array($curl, array(
CURLOPT_URL => $session_url ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Accept: application/json",
"username: USER",
"password: PASS",
"authorizationkey: KEY"
),
));
$response = curl_exec($curl);
curl_close($curl);
$session_id = $response;
} add_filter( 'gform_after_submission_4', 'get_session_id' );
function get_field_data_id() {
global $session_id;
$curl = curl_init();
$file_url = "URL";
curl_setopt_array($curl, array(
CURLOPT_URL => $file_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Accept: application/json",
"Session: {$session_id}"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;}
However when I run this function, I get an error message that the session ID is invalid. What am I missing?
{"Message":"Invalid Authorization. Session ID is invalid. "}

CURLOPT_HTTPHEADER does not set

Using curl to call API GET method with headers
but header not working
Response -- Failed to connect to 52.172.133.9: Permission denied
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "25002",
CURLOPT_URL => "https://api.eko.in:25002/ekoicici/v1/customers/mobile_number:8734818474?initiator_id=8734818474",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => "CURL_HTTP_VERSION_1_1",
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"developer_key: $developerKey",
"secret-key: $secret_key",
"secret-key-timestamp: $secret_key_timestamp"
)
));
$response = curl_exec($curl);
var_dump($response);
$err = curl_error($curl);
var_dump($err);
var_dump("developerKey--".$developerKey);
var_dump("secret_key--".$secret_key);
var_dump("secret_key_timestamp--".$secret_key_timestamp);
curl_close($curl);
when hitting the request on this URL it requires key that's pass on the header but the header does not set on curl.
how to fix that's a problem...

Get all cookies from a page using cURL PHP

I'm trying to access a page using PHP's cURL, this page is to return 2 cookies in the "Response Headers", but in the cURL header only returns the first one.
Does anyone know if there might be some block on the page, and how to work around this problem?
This cookie is generated on a "Status Code: 302"
I've already tried to get "file_get_contents" and also tried to do the
"CURLOPT_HEADERFUNCTION" function and I also know success
OBS: The function to break the google captcha is already working.
$url = 'https://pje.trt15.jus.br/captcha/login_post.php';
$post = [
'g-recaptcha-response' => $g_response,
'referer' => '/consultaprocessual/pages/consultas/ConsultaProcessual.seam',
'random' => $g_captcha_random,
'entrar' => $g_captcha_entrar
];
if( $g_captcha_enviar != null )
$post[$g_captcha_enviar] = 'Enviar';
if( $g_captcha_entrar != null )
$post['entrar'] = $g_captcha_entrar;
$post_http = http_build_query($post);
$headers = [
':authority: pje.trt15.jus.br',
':method: POST',
':path: /captcha/login_post.php',
':scheme: https',
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
//'accept-encoding: gzip, deflate, br',
'accept-language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
'cache-control: no-cache',
'content-length: ' . strlen($post_http),
'content-type: application/x-www-form-urlencoded',
'cookie: '.$cookie_captchasess.' _ga=GA1.3.1830456077.1560269951; _gid=GA1.3.249690674.1560269951',
'origin: https://pje.trt15.jus.br',
'referer: https://pje.trt15.jus.br/consultaprocessual/pages/consultas/ConsultaProcessual.seam',
'upgrade-insecure-requests: 1'
];
$options = [
CURLOPT_COOKIESESSION => true,
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post_http,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_COOKIE => $cookie_captchasess,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_VERBOSE => true,
CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS,
CURLOPT_ENCODING => 'gzip, deflate',
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 40,
CURLOPT_USERAGENT => $user_agent,
];
$ch = curl_init();
curl_setopt_array($ch, $options);
$resp = curl_exec($ch);
curl_close($ch);
echo "<pre>";
echo $resp;

Categories