GameAnalytics metric api curl post example - php

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.`

Related

Why bitbucket server checks Token, even if its blocked?

we have Atlassian Bitbucket Server. And I'm trying to use its API.
Found this: https://docs.atlassian.com/bitbucket-server/rest/7.21.0/bitbucket-rest.html#idp299
Using PHP, i tried:
$url = "http://10.77.78.235:7990/rest/api/1.0/projects/HAL/repos/crm-2/branches";
$headers = array(
'cache-control: max-age=0',
'upgrade-insecure-requests: 1',
'user-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36',
'sec-fetch-user: ?1',
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'x-compress: null',
'sec-fetch-site: none',
'sec-fetch-mode: navigate',
'accept-encoding: deflate, br',
'accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
"Content-Type: application/json",
'X-Atlassian-Token: no-check',
);
$post_data = array (
"name" => "bar",
"startPoint" => "52005525378526ac3d5e5bcffc48fc9a82ebca76",
"message" => "Submit"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "myuser:mypassword");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
And the answer i recieve is:
XSRF check failed
Who knows why - please help)
Kinda stupid solution:
I just had to remove this:
'user-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
From headers.

Get the value of CURLOPT_HTTPHEADER from cURL

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_

How to send POSTFIELDS to cURL correctly

I need to send POST data via cURL as shown in the picture.
image with POST data
i have this code
$data = [
'action' => 'order_cost',
'address' => 'http://91.211.117.3:720'
];
$query = http_build_query($data);
$url = "https://ap4.taxi/api/TaxiAPI.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvJFySHvqeKppEN9W',
)
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);
but I get an error
image with error
I have already tried many options. Postman sends POST normally and I receive the answer.
Please tell me I can not even imagine how this can be done.
As I see from your code you are sending just two fields by POST method (action and address)
Please show us a code of https://ap4.taxi/api/TaxiAPI.php where you process received data.
function execute_curl($url, $curlopt = array()){
$ch = curl_init();
$strCookie = session_name().'='.session_id().'; path=/';
session_write_close();
$default_curlopt = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_COOKIE => $strCookie,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-1.54 Firefox/3.6.13 GTB7.1"
);
$curlopt = $curlopt + $default_curlopt;
curl_setopt_array($ch, $curlopt);
$response = curl_exec($ch);
$errormsg = curl_error($ch);
$errorCode = curl_errno($ch);
$results = array();
if($errormsg)
{
$results['status'] = 'error';
$results['data'] = $errormsg;
$results['errorcodetxt'] = curl_error_codes($errorCode);
}
else
{
$results['status'] = 'success';
$results['data'] = $response;
}
curl_close($ch);
return $results;
}
$curlopt = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => 1);
$curlresponse = execute_curl($url, $curlopt);

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', '/' );

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 :

Categories