PHP run a curl command - php

I have this curl code
curl -X POST https://url.com -H 'authorization: Token YOUR_Session_TOKEN' -H 'content-type: application/json' -d '{"app_ids":["com.exmaple.app"], "data" : {"title":"Title", "content":"Content"}}
that is used for push notification from web service to a mobile application. How can I use this code in PHP? I can't understand the -H and -d tags

You can use this website to convert any of such:
https://incarnate.github.io/curl-to-php/
But basically d is the payload (the data which you send with the request: usually POST or PUT); H stands for headers: each entry is another header.
So the most 1-to-1 example would be:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://url.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"app_ids\":[\"com.exmaple.app\"], \"data\" : {\"title\":\"Title\", \"content\":\"Content\"}}");
$headers = array();
$headers[] = 'Authorization: Token YOUR_Session_TOKEN';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
but you can make it more dynamic and easy to manipulate based PHP variables by first creating an array with the attributes and then encoding it:
$ch = curl_init();
$data = [
'app_ids' => [
'com.example.app'
],
'data' => [
'title' => 'Title',
'content' => 'Content'
]
];
curl_setopt($ch, CURLOPT_URL, 'https://url.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$headers = array();
$headers[] = 'Authorization: Token YOUR_Session_TOKEN';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
I suggest reading the manual of php-curl:
https://www.php.net/manual/en/book.curl.php

You may also do it in the following way:
<?php
$url = "http://www.example.com";
$headers = [
'Content-Type: application/json',
'Authorization: Token YOUR_Session_TOKEN'
];
$post_data = [
'app_ids' => [
"com.exmaple.app"
],
'data' => [
'title' => 'Title',
'content' => 'Content'
],
];
$post_data = json_encode($post_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
// For debugging
$info = curl_getinfo($ch);
curl_close($ch);
echo '<pre>';
print_r($response);

Related

How to get the list of subscribers from drip account with curl?

I have been trying to get the list of subscribers from drip account. I am trying to do so with the curl php I am unable to do so.
Official example
curl -H 'User-Agent: Your App Name (www.yourapp.com)' \
-u f4ff6a200e850131dca1040cce1ee51a: \
-d status=active \
https://api.getdrip.com/v2/9999999/campaigns
My Code
$TOKEN='f69444e104aea5b77a969bb313852dc1';
$ch = curl_init('https://api.getdrip.com/v2/1186104/subscribers');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'TestApp (laflechee#gmail.com)');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $TOKEN
));
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $data;
I am adding a new subscriber in drip with the below code. You can follow it.
$ch = curl_init();
//YOUR-ACCOUNT-ID replace it with your account id
curl_setopt($ch, CURLOPT_URL, 'https://api.getdrip.com/v2/YOUR-ACCOUNT-ID/subscribers');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$data = array(
'subscribers' => array(
array(
'email' => 'myemail#domain.com',
'first_name' => 'Mohsin',
'last_name' => 'raza'
),
),
);
$post = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
//YOUR-API-TOKEN-HERE replace it with your api-token
curl_setopt($ch, CURLOPT_USERPWD, 'YOUR-API-TOKEN-HERE' . ':' . '');
$headers = array();
//replace with your app name and registered domain with drip account
$headers[] = 'User-Agent: YOUR-APP-NAME (www.your-domain.com)';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo $result;
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Firebase FCM error JSON_PARSING_ERROR: Unexpected token END OF FILE at position

I am trying to send notifications through firebase's fcm service using php. Here is what I got so far:
$ch = curl_init();
$payload = [
'to' => '/topics/'.ANDROID_TOPIC,
'notification' => [
'message' => 1
]
];
$headers = [
'Content-Type: application/json',
'Content-length: '.sizeof(json_encode($payload)),
'Authorization: key='.FIREBASE_KEY
];
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
$result = curl_exec($ch );
curl_close($ch);
return $result;
However, I am getting Unexpected token END OF FILE at position 2 response from firebase.
What do you think is the reason this is happening?
Remove Content-length line:
$header = array();
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: key=' . FIREBASE_KEY;
$payload = [
'to' => 'verybigpushtoken',
'notification' => [
'title' => "Portugal VS Germany",
'body' => "1 to 2"
]
];
$crl = curl_init();
curl_setopt($crl, CURLOPT_HTTPHEADER, $header);
curl_setopt($crl, CURLOPT_POST,true);
curl_setopt($crl, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($crl, CURLOPT_POSTFIELDS, json_encode( $payload ) );
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true );
// curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, false); should be off on production
// curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false); shoule be off on production
$rest = curl_exec($crl);
if ($rest === false) {
return curl_error($crl)
}
curl_close($crl);
return $rest;

Sending Push Notifications via OneSignal using PHP

I am using the following PHP Code to send push notification via One Signal:
function sendMessage(){
$content = array(
"en" => 'English Message'
);
$fields = array(
'app_id' => $appId,
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$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 '.$restKey));
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);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");
And I am getting the following response:
JSON sent: {"app_id":null,"included_segments":["All"],"data":{"foo":"bar"},"contents":{"en":"English Message"}} JSON received: "{\"allresponses\":\"{\\\"adm_big_picture\\\":null,\\\"adm_group\\\":null,\\\"adm_group_message\\\":null,\\\"adm_large_icon\\\":null,\\\"adm_small_icon\\\":null,\\\"adm_sound\\\":null,\\\"amazon_background_data\\\":false,\\\"android_accent_color\\\":null,\\\"android_group\\\":null,\\\"android_group_message\\\":null,\\\"android_led_color\\\":null,\\\"android_sound\\\":null,\\\"android_visibility\\\":null,\\\"app_id\\\":\\\"63c6c8f7-694b-4c68-abc1-d820d9bbbec1\\\",\\\"big_picture\\\":null,\\\"buttons\\\":null,\\\"canceled\\\":false,\\\"chrome_big_picture\\\":null,\\\"chrome_icon\\\":null,\\\"chrome_web_icon\\\":\\\"\\\",\\\"chrome_web_image\\\":\\\"\\\",\\\"content_available\\\":false,\\\"contents\\\":{\\\"en\\\":\\\"This is a new message.\\\"},\\\"converted\\\":0,\\\"data\\\":null,\\\"delayed_option\\\":\\\"immediate\\\",\\\"delivery_time_of_day\\\":\\\"4:00 PM\\\",\\\"errored\\\":0,\\\"excluded_segments\\\":[],\\\"failed\\\":0,\\\"firefox_icon\\\":\\\"\\\",\\\"headings\\\":{\\\"en\\\":\\\"New Message\\\"},\\\"id\\\":\\\"8d56f592-8f43-461a-94e8-2fe9922ba844\\\",\\\"include_player_ids\\\":null,\\\"included_segments\\\":[\\\"All\\\"],\\\"ios_badgeCount\\\":null,\\\"ios_badgeType\\\":null,\\\"ios_category\\\":null,\\\"ios_sound\\\":null,\\\"isAdm\\\":false,\\\"isAndroid\\\":false,\\\"isChrome\\\":false,\\\"isChromeWeb\\\":true,\\\"isFirefox\\\":true,\\\"isIos\\\":false,\\\"isSafari\\\":true,\\\"isWP\\\":false,\\\"isWP_WNS\\\":false,\\\"large_icon\\\":null,\\\"priority\\\":null,\\\"queued_at\\\":1492523636,\\\"remaining\\\":0,\\\"send_after\\\":1492523636,\\\"small_icon\\\":null,\\\"successful\\\":3,\\\"tags\\\":null,\\\"filters\\\":null,\\\"template_id\\\":null,\\\"ttl\\\":null,\\\"url\\\":\\\"\\\",\\\"web_buttons\\\":null,\\\"wp_sound\\\":null,\\\"wp_wns_sound\\\":null}\"}"
However the Push Notification is not appearing in the One Signal Dashboard and neither being received by those who subscribed.
Can someone help please :) ?
your mistake is here.
'Authorization: Basic '.$restKey));
Correct one is.....
'Authorization: Basic "'.$restKey."'"));
I am working this code and this is working fine
function SendOneSignalMessage($message,$empid){
// Your code here!
$fields = array(
'app_id' => 'xxxxxxxxxxxxxxxxxx',
'include_player_ids' => [$empid],
'contents' => array("en" =>$message),
'headings' => array("en"=>"etc"),
'largeIcon' => 'https://cdn4.iconfinder.com/data/icons/iconsimple-logotypes/512/github-512.png',
);
$fields = json_encode($fields);
//print("\nJSON sent:\n");
//print($fields);
$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 xxxxxxxxxxxxx));
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);
}

php cURL with 'application/json' as Header

I'm trying to do this curl request using php http-build-query but it is not working.
curl -H "Content-Type: application/json" -X POST -d '{"crawlDepth": 1, "url": "http://testphp.vulnweb.com/artists.php?artist=1"}' http://127.0.0.1:8775/scan/
How is the best way to create this request?
$data = array("crawlDepth" => 1, "url" => "http://testphp.vulnweb.com/artists.php?artist=1");
$data_string = json_encode($data);
$ch = curl_init('http://127.0.0.1:8775/scan/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);

Azure API - PHP Request

Im trying to get this API working in PHP
The API documentation is here
https://studio.azureml.net/apihelp/workspaces/3e1515433b9d477f8bd02b659428cddc/webservices/aca8dc0fd2974e7d849bbac9e7675fda/endpoints/cb1b14b17422435984943d41a5957ec7/score
Im really stuck and im so close to getting it to work. Below is my current code if anyone can spot any errors. I have also included my API key as it will be changed once working.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$url = 'https://ussouthcentral.services.azureml.net/workspaces/3e1515433b9d477f8bd02b659428cddc/services/cb1b14b17422435984943d41a5957ec7/execute?api-version=2.0&details=true';
$api_key = '5ve72/xxLuzaexQu7LyRBl1iRdGqAQiQ1ValodnS7DG+F0NzgHkaLyk1J30MXrlWFovzPzlurui/o5jeH7RMiA==';
$data = array(
'Inputs'=> array(
'input1'=> array(
'ColumnNames' => ['Client_ID'],
'Values' => [ [ '0' ], [ '0' ], ]
),
),
'GlobalParameters' => array()
);
$body = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$api_key, 'Accept: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo 'Curl error: ' . curl_error($ch);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
im still getting no error from curl_error and the var dump just says bool(false)
You had an issue with the element GlobalParameters, declare it as a StdClass instead of an empty array. Try this :
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$url = 'https://ussouthcentral.services.azureml.net/workspaces/3e1515433b9d477f8bd02b659428cddc/services/cb1b14b17422435984943d41a5957ec7/execute?api-version=2.0&details=true';
$api_key = '5ve72/xxLuzaexQu7LyRBl1iRdGqAQiQ1ValodnS7DG+F0NzgHkaLyk1J30MXrlWFovzPzlurui/o5jeH7RMiA==';
$data = array(
'Inputs'=> array(
'input1'=> array(
'ColumnNames' => ['Client_ID'],
'Values' => [ [ '0' ], [ '0' ], ]
),
),
'GlobalParameters' => new StdClass(),
);
$body = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$api_key, 'Accept: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $body . PHP_EOL . PHP_EOL;
echo 'Curl error: ' . curl_error($ch);
curl_close($ch);
var_dump($response);
You have to run curl_error() after curl_exec() because curl_error() does return a string containing the last error for the current session. (source : php.net)
So go this way
$response = curl_exec($ch);
echo 'Curl error: ' . curl_error($ch);
And you should have a error telling you what is wrong.

Categories