I need to send a data via cURL in PHP.
There is my POST request and it works with this example (I can post 1 record).
$postData = array(
"username" => "email#domain.com",
"name" => "Name",
"surname" => "Surname",
'role' => 'user',
"disabled" => "false"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getCurlUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt_array($ch,
array(
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'authorization: apikey '.$apikey,
'Content-Type: application/rest+json',
'pincode: '.$pincode
)
// ,CURLOPT_POSTFIELDS => json_encode($postData)
));
curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($postData));
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
$response = curl_exec($ch);
if ($response === false) $response = curl_error($ch);
return stripslashes($response);
curl_close($ch);
But I would like to add records from database into $postData array but then it's stops.
This is my database array example:
$postData = array();
foreach ($records = $this->getRecords as $value) {
$row['username'] = $value['email'];
$row['name'] = $value['name'];
$row['surname'] = $value['surname'];
$row['role'] = 'User';
$row['disabled'] = $value['status'];
array_push($postData, $row);
}
json array looks ok but I can't find an error. Or i can't post multiple data and i need to add everything in a loop?
I find a few flaws in the code. You said that you are posting the code but you have set
CURLOPT_POST => false
Also, you have repeated
CURLOPT_RETURNTRANSFER => TRUE and CURLOPT_RETURNTRANSFER => 1
Also, your API might be designed to accept one record but you are trying to send multiple records. If it is designed to accept 1 record at a time you should make the curl call a function and call it in place of array_push. eg:. curlfunction($row);
function curlfunction($postData){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getCurlUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST , 1);
curl_setopt_array($ch, CURLOPT_HTTPHEADER, array(
'authorization: apikey '.$apikey,
'Content-Type: application/rest+json',
'pincode: '.$pincode
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($postData));
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
$response = curl_exec($ch);
if ($response === false) $response = curl_error($ch);
curl_close($ch);
return stripslashes($response);
}
and
foreach ($records = $this->getRecords as $value) {
$row['username'] = $value['email'];
$row['name'] = $value['name'];
$row['surname'] = $value['surname'];
$row['role'] = 'User';
$row['disabled'] = $value['status'];
curlfunction($row);
}
Related
when i try to make a request implementing curl_setopt_array it doesn't work and sophos returns an error:
<?php
try {
$url = 'http://127.0.0.1/index.php';
$fields = array(
'JsonData'=>json_encode(['test' => 'test'])
);
$postvars = http_build_query($fields);
$options = [
CURLOPT_URL => $url,
CURLOPT_POST => count($fields),
CURLOPT_POSTFIELDS => $postvars,
CURLOPT_TIMEOUT => 2,
CURLOPT_RETURNTRANSFER => false,
CURLOPT_FORBID_REUSE => true,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_DNS_CACHE_TIMEOUT => 10,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_HTTPHEADER => array('Connection: close')
];
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
echo 'Finish!!!'
} catch (Exception $e) {
echo $e->getMessage();
}
note: curl_setopt_array return true but, when perform the curl_exec my sophos firewall return error:
Error: Content could not be delivered fue to the folloeing condition: Invalid argument
when i tried to make a request by implementing curl_setopt works correctly and not get a sophos interruption:
<?php
try {
$url = 'http://127.0.0.1/index.php';
$fields = array(
'JsonData'=>json_encode(['test' => 'test'])
);
$postvars = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
curl_exec($ch);
curl_close($ch);
echo 'Finish!!!'
} catch (Exception $e) {
echo $e->getMessage();
}
Why the second works correctly and the first does not work? From my point of view I'm doing the same...
My PHP script looks like this:
<?php
$reg_id = "d8Sq53-gteU:APA91bGFcbSrcWY6J9fVBhUJVci4YHgktjoTOTbRjMXi7uY6ss-kLM39GpSt16cMmwsm2k4n9y3_YrcyBT7o9bpsN2QFS_bVceMcV-WThbThXMCWSiwaaP7p5LAJlb_01mzPbHb6xq1X1";
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => $reg_id ,
'priority' => "high",
'data' => array(
"title" => "Android Learning",
"message" => "Test",
"image"=> "dsdsd",
"tag" => "dsdsd"
)
);
$headers = array(
'Authorization:key = AIzaSyC6ld4WBRmk8W6DZgMqevu1Na3dcQdQDBIA ',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
print_r($result);die;
?>
This is the response I am getting:
{"multicast_id":7558168491201020947,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1484883356821016%9bd11ceef9fd7ecd"}]}
But in Android I am unable to get the data that I am posting through the notification. Is there a problem with PHP script that I am using, Is the response that I am getting through PHP script correct. Or there is some problem with the android code. Can anyone help me please.
**Please check below .php file it will working fine for me.**
**You just need to pass firebase id "fcm_token" parameter to this php file**.
<?php
require_once __DIR__ . '/config.php';
// need to pass Firebase Register ID.
$registration_ids=$_POST["fcm_token"];
$title='hello';
$message='Please check the Details';
$is_background=FALSE;
$image='';
$payload='Its Payload';
$timestamp='10:15';
$arr = array('title' => $title, 'is_background' => $is_background, 'message' => $message, 'timestamp' => $timestamp, 'image' => $image,'payload'=> $payload);
$arr1 = array('data' => $arr);
$json = $arr1;
$fields = array('to' => $registration_ids,'data' => $json,);
//echo json_encode($fields);
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Authorization: key=' . FIREBASE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
echo 'its Not done bro';
}else{
echo 'its done bro';
}
// Close connection
curl_close($ch);
?>
Finally got the code which is working for me-
`
$filename = 'Fav_Icon.png';
$title = "thisis title";
$coupon_id = 1;
$url = 'https://fcm.googleapis.com/fcm/send';
$msg = array
(
'message' => 'We have added a new Coupon. Please have a look !!!',
'title' => $title,
'smallIcon' => base_url().'uploads/icons/'.$filename,
'type' => 'Coupon',
'coupon_id' => $coupon_id
);
$res = array();
$res['data']['title'] = "Coupon Name";
$res['data']['message'] = "We have added a new Coupon. Please have a look !!!";
$res['data']['image'] = base_url().'uploads/icons/'.$filename;
$res['data']['tag'] = "Coupon";
$res['data']['coupon_id'] = $coupon_id;
$fields = array(
'to' => $reg_id ,
'priority' => "high",
'data' => $res
);
$headers = array(
'Authorization:key = AIzaSyC6ld4WBRmk8W6DZgMqevu1Na3dcQdQDBIER ',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
print_r($result);
return $result;
?>`
i want to receive errors from the sms host server
curl_error curl_errno curl_strerror all these function returns error but not in the way i want , mysmshost say that you will receive code like, 202 for wrong mobile number, 301 for wrong authentication, i want to receive those error code please help.
this is my code.
//sending message
$authKey = "my_authentication_key";
$senderId = "sender";
$message = urlencode("Hello Its Working..");
$route = "1";
$campaign = 'Default';
//Prepare you post parameters
$postData = array(
'authkey' => $authKey,
'mobiles' => $number,
'message' => $message,
'sender' => $senderId,
'route' => $route,
'campaign' => $campaign
);
//API URL
$url="http://sms.mysmshost.com/sendhttp.php";
// init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//get response
$output = curl_exec($ch);
//Print error if any
if(curl_errno($ch))
{
echo curl_error($ch)."-curl_error<br/>";
$chcode =curl_errno($ch);
echo $chcode;
echo '<br/>error:' . curl_strerror($chcode)."<br/>";
}
curl_close($ch);
echo $output;
Curl_errorno() is not executing, even if i print them without if condition they give no result.
Try this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$server_output = curl_exec($ch);
curl_close($ch);
print_r($server_output);
Has any one had any success in using Pushwoosh remote api to send custom notifications to a specific device? I have went over their documentation to set this up, but the notification keeps going out to all devices. What am I doing wrong here? Thanks in advance.
<?php
define('PW_AUTH', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('PW_APPLICATION', 'XXXXXX-XXXXXX');
define('PW_DEBUG', true);
function pwCall($method, $data = array()) {
$url = 'https://cp.pushwoosh.com/json/1.3/' . $method;
$request = json_encode(['request' => $data]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (defined('PW_DEBUG') && PW_DEBUG) {
print "[PW] request: $request\n";
print "[PW] response: $response\n";
print "[PW] info: " . print_r($info, true);
}
}
pwCall('createMessage', array(
'application' => PW_APPLICATION,
'auth' => PW_AUTH,
'notifications' => array(
array(
'send_date' => 'now',
'content' => 'Send this content to user',
)
),
'devices' => array('2lksdflkje96a4389f796173fakeae938device95ajkdh8709843') //Optional. Not more than 1000 tokens in an array. If set, message will only be delivered to the devices in the list. Ignored if the applications group is used
)
);
?>
You must specify devices list into notifications array. Please see right request below (Also Pushwoosh API documentation available here https://www.pushwoosh.com/programming-push-notification/pushwoosh-push-notification-remote-api/#PushserviceAPI-Method-messages-create)
<?php
define('PW_AUTH', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('PW_APPLICATION', 'XXXXXX-XXXXXX');
define('PW_DEBUG', true);
function pwCall($method, $data = array()) {
$url = 'https://cp.pushwoosh.com/json/1.3/' . $method;
$request = json_encode(['request' => $data]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (defined('PW_DEBUG') && PW_DEBUG) {
print "[PW] request: $request\n";
print "[PW] response: $response\n";
print "[PW] info: " . print_r($info, true);
}
}
pwCall('createMessage', array(
'application' => PW_APPLICATION,
'auth' => PW_AUTH,
'notifications' => array(
array(
'send_date' => 'now',
'content' => 'Send this content to user',
'devices' => array('2lksdflkje96a4389f796173fakeae938device95ajkdh8709843') //Optional. Not more than 1000 tokens in an array. If set, message will only be delivered to the devices in the list. Ignored if the applications group is used
)
),
)
);
?>
i have problem with this website api,
i used this code to generate a shorturl by this api but always i got a White Page Screen without any content!
Please Help me to generate a url:
<?php
function bucksapi($longUrl)
{
$bucksapi = 'myapipass';
$sinoone = 'myusername';
$adts = '2';
$contype = '1';
$domainss = 'linkbucks.com';
$postData = array('originalLink' => $longUrl, 'user' => $sinoone, 'apiPassword' => $bucksapi, 'contentType' => $contype, 'adType' => $adts, 'domain' => $domainss);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.linkbucks.com/api/createLink/single');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
//As the API is on https, set the value for CURLOPT_SSL_VERIFYPEER to false. This will stop cURL from verifying the SSL certificate.
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
$json = json_decode($response);
curl_close($curlObj);
return $json->link;
}
?>
and this for printing the short link:
<?php
$long_url = "http://google.com";
echo bucksapi($long_url);
?>
Based on your work.
<?php
function curl($url, $cookies= NULL, $post = NULL)
{
$ch = #curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
if (!empty($cookies)) {
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (!empty($post)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 25);
$page = curl_exec($ch);
var_dump($page);
curl_close($ch);
return $page;
}
function bucksapi($longUrl, $bucksapi, $sinoone, $adts = '2', $contype = '1')
{
$postData = array(
'originalLink' => $longUrl,
'user' => $sinoone,
'apiPassword' => $bucksapi,
'contentType' => $contype,
'adType' => $adts,
'domain' => 'linkbucks.com'
);
$json = json_decode(
curl(
'https://www.linkbucks.com/api/createLink/single',
NULL,
json_encode($postData)
)
);
var_dump($json);
return $json->link;
}
var_dump( bucksapi( "LINK_URL", 'API_KEY', 'USER_NAME' ) );
?>