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;
Related
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?
I'm running a cURL post request that is appearing to hit the url and return a response. However, I'm led to believe that I'm not handling the request output properly and it's throwing an error that I don't understand. I'm extremely new to cURL and if anyone could point me in the right direction, I'd appreciate it.
$curl = curl_init();
$headers = array(
'Content-Type: application/json',
'X-Requested-With: XMLHttpRequest',
);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'XXX',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => [
client_id => 'XXX',
client_secret => 'XXX',
member_id => 'XXX'
],
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
]);
$resp = curl_exec($curl);
curl_close($curl);
var_dump(json_decode($resp, true));
This results in the errors that follows:
{"errors":{"":["Input string '--------------------------cec6101ba64bcb7f' is not a valid number. Path '', line 1, position 42."]},"title":"One or more validation errors occurred.","status":400,"traceId":"80031695-0002-ef00-b63f-84710c7967bb"}
$curl = curl_init();
$headers = array(
'Content-Type: application/json',
'X-Requested-With: XMLHttpRequest',
);
//////
Adding this and using json_encode allowed for the request to be properly sent with the required id's and secret tokens.
//////
$data = array(
"ClientId" => "$api_clientId" ,
"ClientSecret" => "$api_clientSecret",
"MemberId" => "$api_memberId"
);
$data_string = json_encode($data);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://cors-anywhere.herokuapp.com/https://staging.micromerchantsystems.com/authenticationservice/api/GateKeeper/createtoken',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
]);
$resp = curl_exec($curl);
curl_close($curl);
var_dump(json_decode($resp, true));
I am trying send a curl request to a URL which requires HTTP_ORIGIN to be set, I have this so far...
$headers = array(
'Origin: www.myorigin.com',
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER, true,
CURLOPT_HTTPHEADER, $headers,
CURLOPT_URL => 'http://www.example.com',
CURLOPT_USERAGENT => 'Sample Request'
));
$resp = curl_exec($curl);
curl_close($curl);
This is giving me an error on the server side of Undefined index: HTTP_ORIGIN so it doesn't look like it is passing the origin through.
Have I set this up correctly?
You use coma , instead of => into your array, so CURLOPT_HTTPHEADER is a value of the array, instead of a key.
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => true, // << HERE
CURLOPT_HTTPHEADER => $headers, // << HERE
CURLOPT_URL => 'http://www.example.com',
CURLOPT_USERAGENT => 'Sample Request'
));
I'm trying to access a third-party API with PHP.
Their command reference is like so:
curl "https://api.example.com/api/" \
-H "Authorization: BEARER-TOKEN" \
-F 'data={"language":"EN","latitude":12.345,"longitude":12.345}'
I've tried:
$url = 'https://api.example.com/api/';
$header = array(
'Content-type: application/json',
'Authorization: ' . $bearertoken
);
$fields = 'data={"language":"EN","latitude":"'.$latitude.'","longitude":"'.$longitude.'"}';
// I've tried this too:
$fields = array('data' => '{"language":"EN","latitude":"'.$latitude.'","longitude":"'.$longitude.'"}');
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $fields,
CURLOPT_HTTPHEADER => $header,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);`
But the server responds with a message telling me it didn't get the parameters I'm sending. When I send the same -F "data={}" from a terminal I get the correct response.
You shouldn't use Content-type: application/json. The value of the data parameter is JSON, but the parameters as a whole are sent in application/x-www-form-urlencoded format.
You also need to URL-encode the value of the data parameter.
The best solution is to use json_encode to create the JSON value, then use an array of post fields, then curl will encode the fields properly.
$header = array(
'Authorization: ' . $bearertoken
);
$fields = array('data' =>
json_encode(array('language' => 'EN',
'longitude' => $longitude,
'latitude' => $latitude)));
curl -F submits the form using Content-Type: multipart/form-data, following RFC 2388. You're specifically contravening that when you set the application/json content type.
$data = array('language' => 'EN', 'longitude' => $longitude, 'latitude' => $latitude);
$headers = array (
"Content-Type: multipart/form-data",
"Authorization: $bearertoken",
);
$postfields = array ("data" => json_encode($data));
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $options);
curl_exec($ch);
Also, if that's the actual documentation, it may be wrong. The format for Authorization header is Authorization: Bearer <token>.
Working on a CURL Post to a restful backend service. Using RESTClient extension in Firefox I get my intended response.
So I have my Authorization, Accept, and Content-Type Headers set in CURL
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonpoststring,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo#bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec($ch);
curl_close($ch);
In RESTClient I have this JSON in the Body
{"authentication":{"identification":{"identstring":"foo#bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}
My question is: How to include that body in the PHP Curl request.
Pass it in postfields
jsonpoststring='{"authentication":{"identification":{"identstring":"foo#bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}
';
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonpoststring,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo#bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec($ch);
curl_close($ch);
This should do the trick
$data = json_decode('{"authentication":{"identification":{"identstring":"foo#bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}');
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo#bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec