Get the value of CURLOPT_HTTPHEADER from cURL - php

I have the method below that is to get the response from webserver using cURL.
function login (string $_login, string $_password) : string {
$url = "https://acweb.net.br/api/orcamentos/login";
$fields = [
"login" => $_login,
"password" => $_password
];
$headers = [
"Try" => "Trying"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return curl_exec( $ch );
}
It works fine!
i can get the value of $_POST with
print_r ($_POST)
But i can't get the value of CURLOPT_HTTPHEADER.
EDIT:
I did try so:
print_r ($_SERVER)
but it wasn't there.
How can i get the value of CURLOPT_HTTPHEADER?
all HTTP_headers in $_SERVER:
[HTTP_HOST] => ctemcasb.com.br
[HTTP_CONNECTION] => keep-alive
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
[HTTP_SEC_FETCH_USER] => ?1
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
[HTTP_SEC_FETCH_SITE] => none
[HTTP_SEC_FETCH_MODE] => navigate
[HTTP_ACCEPT_ENCODING] => gzip, deflate, br
[HTTP_ACCEPT_LANGUAGE] => pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7
[HTTP_COOKIE] => PHPSESSID=j4cqqdc83fia68nk0gsglqk1bv
HTTP_TRY, not exists.
And now?
I did this in the server:
print_r($_SERVER)
and
print_r ($_SERVER["HTTP_TRY]);

The headers shouldn't be an associative array, it should be an indexed array of strings.
$headers = [
'Try: Trying',
'Content-Type: text/html',
...
];
Then you should be able to access the header with: $_SERVER['HTTP_TRY'] since custom headers are prefixed with HTTP_

Related

GameAnalytics metric api curl post example

function getData()
{
$ch = curl_init();
$url = 'https://metrics.gameanalytics.com/metrics/v1/metrics/ad_impressions_per_user?';
$key = 'myapikeyhere';
$fields = array(
"granularity" => "day",
"interval" => "2022-10-21T04:00:00.000Z/2022-11-21T04:00:00.000Z",
"query" => array(
"dimension" => "ad_type",
"limit" => 3,
"type" => "group"
)
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Accept-Encoding: gzip, deflate',
'Connection: close',
'Host: metrics.gameanalytics.com',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
"X-API-Key: $key",
)); //you can specify multiple custom keys.
$result = curl_exec($ch);
curl_close($ch);
print_r(json_decode($result));
}
` its my code i always getting (415 Unsupported Media Type). i need solution or some example
gameanalytics metric api using curl php.`

Would a misconfiguration in cookies and/or postfields lead to this 500 internal server error when using wp_remote_post to post data within Wordpress?

I am connecting to a server that does not have a published API and have successfully been able to use the PHP curl functions to retrieve a token and login, but when I try to use Wordpress built-in functions to achieve the same thing, I am not successful and receive a 500 Internal Server Error from the remote host. I believe that I may need to properly configure the cookies and/or data parameters.
I am able to retrieve a token from the remote host using wp_remote_get. I have tried changing the Content-Type to text, json and other common alternatives, although 'application/x-www-form-urlencoded' worked fine when using curl. I have tried sending the token in the header as well as in post fields.
// I am trying to get this to work:
$token = get_transient('access_token');
if (!$token)
return;
$args = array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'compress' => true,
'headers' => array(
'Authority' => 'remote.site.com',
'Content-Type' => 'application/x-www-form-urlencoded',
'Referer' => 'remote.site.com/login',
'Cache-Control' => 'max-age=0',
'Upgrade-Insecure-Requests' => 1,
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Accept-Encoding' => 'gzip, deflate, br',
'Accept-Language' => 'zh-CN,zh;q=0.9,en;q=0.8,ms;q=0.7,zh-TW;q=0.6,fr;q=0.5',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'
),
'cookies' => array(),
'body' => array(
'__RequestVerificationToken' => $token,
'UserName' => USERNAME,
'Password' => PASSWORD,
'RememberMe' => true
)
);
$response = wp_remote_post( 'remote.site.com', $args);
// and no issue doing it this way...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'remote.site.com/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "__RequestVerificationToken=$token&UserName=JohnDoe&Password=password&RememberMe=true");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Authority: remote.site.com';
$headers[] = 'Cache-Control: max-age=0';
$headers[] = 'Origin: https://remote.site.com';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
$headers[] = 'Referer: https://remote.site.com/login';
$headers[] = 'Accept-Encoding: gzip, deflate, br';
$headers[] = 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,ms;q=0.7,zh-TW;q=0.6,fr;q=0.5';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_HEADER, 1);
When using curl functions, I get 302 response for the login request and subsequent requests are fulfilled, whereas with wp_remote_post I get a response with the login page and a 500 internal server error response.
add code inside the wp-config file
define('COOKIE_DOMAIN', $_SERVER['HTTP_HOST'] );
define( 'ADMIN_COOKIE_PATH', '/' );
define( 'COOKIEPATH', '/' );
define( 'SITECOOKIEPATH', '/' );

How to start session with php code

I would like to send request to server. I am using this code for it
$data=[..];
$header=[
"Accept-language: en-US,en;q=0.5",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Connection: keep-alive",
"Host: www.rvvi.cz",
"Referer: https://www.rvvi.cz/riv?s=rozsirene-vyhledavani",
"Upgrade-Insecure_Requests:1",
"User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/2010",
"Cookie: PHPSESSID=4ck9tc3vm4prgfubnjvutilgd2",
"Content-type: application/x-www-form-urlencoded"
];
$url = "https://www.rvvi.cz/riv";
$query = http_build_query($data);
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST,strlen($query));
curl_setopt($ch,CURLOPT_POSTFIELDS,$query);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$page=curl_exec($ch);
echo($page);
but problem is with PHPSESSID in header. To access that, i have to manually go to this server with my browser ( this that probably start my SESSION ), copy PHPSESSID and paste it into my script. But, i would like to make this script automatic. I need to to active them from my server without using browser or copying something.
Is there any way how can i start this session just with my php code?
$curl = curl_init( 'https://httpbin.org/post' );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, array( 'field1' => 'some data', 'field2' => 'some more data' ) );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec( $curl );
curl_close( $curl );

file_get_content don't work for special site

i want to get content of https://06fazmusic.com/ but file_get_content don't work.
$context = stream_context_create(array(
'http' => array(
'method' => "GET",
'follow_location' => false,
'header' => "Accept-Language: en-US,en;q=0.8rn" .
"Accept-Encoding: gzip,deflate,sdchrn" .
"Accept-Charset:UTF-8,*;q=0.5rn" .
"Accept-Language:en-US,en;q=0.8" .
"Cache-Control:max-age=0" .
"User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
)
));
$post_url='https://06fazmusic.com/omid-called-namaze-eshgh/';
$array = get_headers($post_url);
echo file_get_contents($post_url, false, $context);
I didn't use file_get_contents(). I used Curl type to get that page ex code is:
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo curl_get_contents($post_url);
it is working Output screen shot is :

Empty result with PHP Curl, But response is exists with json format

I want to extract data from this URL with Php Curl
https://www.tiket.com/ajax/pingFlightSearch?d=CGK&a=DPS&date=2017-08-18&adult=1&child=0&infant=0&airlines=%5B%22LION%22%5D&subsidy=true&page_view=roundseperate
But i got an empty page.
I check to that link and inspect with mozilla network->xhr and there is an content in response tab.
How I can extract the data from that link with php curl?
Here is my code
$url = 'https://www.tiket.com/ajax/pingFlightSearch?d=CGK&a=DPS&date=2017-08-18&adult=1&child=0&infant=0&airlines=%5B%22LION%22%5D&subsidy=true&page_view=roundseperate';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($cURL, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($cURL, CURLOPT_AUTOREFERER, true);
curl_setopt($cURL, CURLINFO_HEADER_OUT, true);
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cURL, CURLOPT_VERBOSE,true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
"Host" => "www.tiket.com",
"User-Agent" => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0",
"Accept" => "application/json, text/javascript, */*; q=0.01",
"Accept-Language" => "en-us,en;q=0.5",
"Accept-Encoding" => "gzip, deflate, br",
"Content-Type" => "application/cap+xml;charset=utf-8",
"X-NewRelic-ID" => "UQIGUlJXGwACUFZaAAM=",
"X-Requested-With" => "XMLHttpRequest",
"Connection" => "keep-alive",
"Pragma" => "no-cache",
"Cache-Control" => "no-cache"
));
$result = curl_exec($cURL);
print_r($result);
curl_close($cURL);
I must add 'X-Requested-With: XMLHttpRequest' at header of that link then response will come out.
edit header
Then the response will come out
[response result][2]
You've done a mistake with CURLOPT_HTTPHEADER. You set params as Key => Value but it's incorrect. You should set params as values with ":" as the delimiter.
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
"Host: www.tiket.com",
"Accept: application/json, text/javascript, */*; q=0.01",
"Accept-Language: en-us,en;q=0.5",
"Content-Type: application/cap+xml;charset=utf-8",
"X-NewRelic-ID: UQIGUlJXGwACUFZaAAM=",
"X-Requested-With: XMLHttpRequest",
"Connection: keep-alive",
"Pragma: no-cache",
"Cache-Control: no-cache"
));
Also I have removed this string to get uncompressed data
"Accept-Encoding: gzip, deflate, br",
Mate are u sure that this link work? because I inspected the code with mozilla and chrome and the response is empty, and I tried to scrape with a simple php class that i have with curl and the result is empty cuz the link dont have response

Categories