Cloudflare API - Create DNS (NS) record using CURL PHP - php

I am working on adding records to my zone using curl (PHP); I want to add test.gen.ng as the DNS record with nameservers (ns1.test.com, ns2.test.com, ns3.test.com, ns4.test.com). I am able to add A records, CNAME records and others with the code at the bottom but not NS records.
{"success":false,"errors":[{"code":1004,"message":"DNS Validation Error","error_chain":[{"code":9102,"message":"content must be a string."}]}],"messages":[],"result":null}
I am having errors as shown above; sending the content below.
<?php
/* Cloudflare.com | APİv4 | Api Ayarları */
$apikey = '61543253246426e7147'; // Cloudflare Global API
$email = 'rose#gen.ng'; // Cloudflare Email Adress
$domain = 'gen.ng'; // zone_name // Cloudflare Domain Name
$zoneid = 'a30b057777777777777777dadd'; // zone_id // Cloudflare Domain Zone ID
$dnsadgeldi = 'test';
$dnsnameserver1 = 'ns1.test.com';
$dnsnameserver2 = 'ns2.test.com';
$dnsnameserver3 = 'ns3.test.com';
$dnsnameserver4 = 'ns4.test.com';
$ttl = 120;
// A-record oluşturur DNS sistemi için.
$ch = curl_init("https://api.cloudflare.com/client/v4/zones/".$zoneid."/dns_records");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Auth-Email: '.$email.'',
'X-Auth-Key: '.$apikey.'',
'Cache-Control: no-cache',
// 'Content-Type: multipart/form-data; charset=utf-8',
'Content-Type:application/json',
'purge_everything: true'
));
// -d curl parametresi.
$data = array(
'type' => 'NS',
'name' => ''.$dnsadgeldi.'',
'content' => Array
(
'0' => $dnsnameserver1,
'1' => $dnsnameserver2,
'2' => $dnsnameserver3,
'3' => $dnsnameserver4
),
'zone_name' => ''.$domain.'',
'zone_id' => ''.$zoneid.'',
'proxiable' => 'false',
'proxied' => false,
'ttl' => 1
);
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_string));
$sonuc = curl_exec($ch);
// If you want show output remove code slash.
// print_r($sonuc);
curl_close($ch);
echo $sonuc;

Related

GA4 custom event from server side, can someone tell me how i can do the following code in php?

const measurement_id = `G-XXXXXXXXXX`;
const api_secret = `<secret_value>`;
fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&`api_secret`=${api_secret}`, {
method: "POST",
body: JSON.stringify({
client_id: 'XXXXXXXXXX.YYYYYYYYYY',
events: [{
name: 'tutorial_begin',
params: {},
}]
})
});
can someone tell me how i can do the above code in php?
i have tried the following but it is not working,
$data = array(
'client_id' => self::ga_extract_cid_from_cookies(),
'user_id' => 123,
'timestamp_micros' => time(),
'non_perosnalised_ads' => false,
'events' => array(
'name' => 'login'
)
);
$dataString = json_encode($data);
$post_url = 'https://www.google-analytics.com/mp/collect?api_secret=xxxxxxx&measurement_id=xxxxxxxxx';
$ch = curl_init($post_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datastring);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
When i run the above its not sending any event to google analytics account. I don't know what is the error and how to find it. Need help on this pls.
I believe the main problem in your example, apart from the typo in the $datastring variable, was that the user id was sent as an int, not as a string. My example here works:
$ip = str_replace('.', '', $_SERVER['REMOTE_ADDR']);
$data = array(
'client_id' => $ip,
'user_id' => '123',
'events' => array(
'name' => 'event_serverside'
)
);
$datastring = json_encode($data);
$post_url = 'https://www.google-analytics.com/mp/collect?api_secret=xxxxxxxxxx&measurement_id=xxxxxxxxx';
$ch = curl_init($post_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datastring);
curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, TRUE);
$result = curl_exec($ch);

OneSignal push notification sending to all user instead of one player

I am trying to send push notification to one device by its player_id stored in my database.
but whine it try it keep sending to all users
i don't know what is wrong in my code
$fields = array(
'app_id' => 'XXXXXXXXXXXXXXXXXXXX',
'include_player_ids ' => $player_ids,
'included_segments' => array(
'All'
),
'data' => $data,
'headings' => $title,
'contents' => $content,
'web_buttons' => []
);
$fields = json_encode($fields);
if($player_ids) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
just remove 'included_segments' => array('All'),
If you fill both of included_segments and include_player_ids, you will send to all selected player ids and also all selected segments. Segment All mean all subscribers.
you can read about other option on official documentation
I need to remove included_segments and web_buttons
both of them not just included_segments , when I try to just remove included_segments it give me error of {"errors":["You must include which players, segments, or tags you wish to send this notification to."]}
also I don't need 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'.

PHP curl POST for Watson Video Enrichment

Am trying to use PHP 7.2 to submit a new job to the Watson Video Enrichment API.
Here's my code:
//set some vars for all tasks
$apiUrl = 'https://api-dal.watsonmedia.ibm.com/video-enrichment/v2';
$apiKey = 'xxxxxxxx';
//vars for this task
$path = '/jobs';
$name = 'Test1';
$notification_url = 'https://example.com/notification.php';
$url = 'https://example.com/video.mp4';
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => "simple.custom-model",
"upload" => array(
"url" => $url
)
);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $apiUrl.$path );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: APIKey '.$apiKey
));
$result = curl_exec($ch);
echo $result;
But I can't get it to work, even with varying CURLOPTs, like:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
I keep getting the response: Bad Request.
Here's the API docs.
Am I setting up the POST CURL all wrong? Is my $data array wrong? Any idea how to fix this?
Reading their API documentation, it looks like the field preset is not of type string. Rather it has a schema defined here. This appears similar to how you are using the upload schema.
You should change your data array to look like this:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"video_url" => "https://example.com/path/to/your/video"
),
"upload" => array(
"url" => $url
)
);
Ok, I figured it out. Thanks to #TheGentleman for pointing the way.
My data array should look like:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"simple.custom-model" => array(
"video_url" => $url,
"language" => "en-US"
)
),
"upload" => array(
"url" => $url
)
);

Error code 410 when polling Skyscanner flight live prices

I am polling live prices from the Skyscanner API. Although I receive the session_key and although I am immediately polling the results I am getting a 410 (Gone) response header with an empty body. It used to work fine from my localhost environment but not on my live server anymore.
Has anybody experienced this before and can maybe give me a hint what the issue could be?
$url_api = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/";
$api_key = "XXX"; // Not shown here
$data = array('apiKey' => $api_key, 'country' => 'DE', 'currency' => 'EUR',
'locale' => 'de-DE', 'originplace' => 'HAM', 'destinationplace' => 'AMS', 'cabinclass' => 'economy', 'outbounddate' => '2017-01-27',
'inbounddate' => '2017-01-30' , 'locationschema' => 'Iata', 'groupPricing' => true);
$httpdata = http_build_query($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_api);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $httpdata);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
$response = curl_exec($ch);
curl_close($ch);
$headers = get_headers_from_curl_response($response);
$url = $headers['Location']."?apiKey=".$api_key."&stops=0";
echo $url;
return;

MailChimp API: Batch Check Subscription Status

What's the best way to check a batch of email addresses and whether or not they are subscribed to a particular list? My current method is making an API call for each email address, but this becomes very slow when the count becomes > 50.
I'm using V2.0, using PHP methods since I'm developing on CakePHP.
//Determine MailChimp Subscription
$args = array(
'id' => $this->apiKeys['mailchimp_list_ID'],
'emails' => array(1 => array('email' => $customer['User']['username']))
);
$mailChimpQuery = $this->mailChimp('member-info', $args);
$mailChimpStatus = ($mailChimpQuery['success_count'] == 1 ? 1 : 0);
$customer['Customer']['subscribed'] = $mailChimpStatus;
The mailChimp call in another Controller is:
public function mailChimp($action, $args=array()) {
$args['apikey'] = $this->apiKeys['mailchimp_api'];
$url = 'https://us8.api.mailchimp.com/2.0/lists/' . $action .'.json';
if (function_exists('curl_init') && function_exists('curl_setopt')){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
$result = curl_exec($ch);
curl_close($ch);
} else {
$json_data = json_encode($args);
$result = file_get_contents($url, null, stream_context_create(array(
'http' => array(
'protocol_version' => 1.1,
'user_agent' => 'PHP-MCAPI/2.0',
'method' => 'POST',
'header' => "Content-type: application/json\r\n".
"Connection: close\r\n" .
"Content-length: " . strlen($json_data) . "\r\n",
'content' => $json_data,
),
)));
}
return $result ? json_decode($result, true) : false;
}
FYI, I came up with my own solution:
Create an array of all the email addresses in the database
Create a for-loop that takes segments of 50 email addresses from that array and runs a batch check using member-info (https://apidocs.mailchimp.com/api/2.0/lists/member-info.php)
Manipulate the return array from the API call to extract only the email address and whether or not it's subscribed to the list.
Within the for-loop, add each 50 email addresses return values into a new array and then by the end of it; you'll have an array of all the email addresses in your database with a corresponding true/false value depending on whether or not they are subscribed to the list.
Good luck!

Categories