I want to write a simple PHP code that will send data to a class in a URL.
I've written code, but it's only for sending data directly to the URL.
How to write this code?
The class name is 'sendmsg'.
<?php
$url = 'https://thxmain.cellmobilen.net:443/Charg/service/ChargingMobile';
$data = array(
'username' => '?',
'password' => '?',
'domain' => '?',
'channel' => '?',
'mobilenum' => '?',
'serviceId' => '?',
'price' => '?',
'trackingId' => '?'
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result !== FALSE) {
var_dump($result);
}else{
print( "Code is wrong" );
}
?>
Related
The developer guide for SuiteCRM is kind of incomplete (at least here in Q4 2017) compared to the old SugarCRM one that it was based on before the software fork. So, by downloading the WordPress Plugin for SugarCRM, I was able to figure out the REST API and JSON for adding a sales lead into SuiteCRM with the following code.
Now how do I prevent duplicates by home phone, mobile phone, or email address?
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
header('Content-Type: text/plain');
CRM::loginCRM('admin','xxxxPASSWORDxxxxxx');
$aResult = CRM::addLead(array(
'name' => 'John Doe',
'description' => 'sample description',
'salutation' => 'Mr.',
'first_name' => 'John',
'last_name' => 'Doe',
'do_not_call' => 'No',
'phone_home' => '202-111-2222',
'phone_mobile' => '202-111-2222',
'email1' => 'test#example.com',
'primary_address_street' => '123 Main Street',
'primary_address_street2' => '',
'primary_address_street3' => '',
'primary_address_city' => 'New York',
'primary_address_state' => 'NY'
));
print_r($aResult);
CRM::logoutCRM();
die('OK');
/////////////////////////
class CRM {
private static $SessionID = '';
private static $URL = 'https://mycrmserver-example.com/service/v4_1/rest.php';
private static $User = '';
private static $Shadow = '';
public static function sendJSON($a) {
$s = file_get_contents(
self::$URL,
false,
stream_context_create(
array(
'http' => array (
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => http_build_query($a)
)
)
)
);
$a2 = json_decode($s);
return $a2;
}
public static function loginCRM($sUser,$sPass) {
$sShadow = md5($sPass);
self::$User = $sUser;
self::$Shadow = $sShadow;
$asLogin = array (
'method' => 'login',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => json_encode(array(
'user_auth' => array(
'user_name' => $sUser,
'password' => $sShadow,
'version' => 1
),
'application_name' => 'RestTest',
'name_value_list' => array()
))
);
$a = self::sendJSON($asLogin);
self::$SessionID = $a->id;
}
public static function logoutCRM() {
$asLogin = array (
'method' => 'logout',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => json_encode(array(
'user_auth' => array(
'user_name' => self::$User,
'password' => self::$Shadow,
'version' => 1
),
'application_name' => 'RestTest',
'name_value_list' => array()
))
);
self::sendJSON($asLogin);
}
public static function addLead($a) {
$asNameValueList = array();
foreach($a as $sKey => $sVal) {
$asNameValueList[] = array('name'=>$sKey,'value'=>$sVal);
}
$asAddEntry = array (
'method' => 'set_entry',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => json_encode(array(
'session' => self::$SessionID,
'module_name' => 'Leads',
'name_value_list' => $asNameValueList
))
);
$a = self::sendJSON($asAddEntry);
return $a;
}
} // end CRM
Add these functions into your CRM class and check them before adding a lead. I had a little help from this answer that incidentally gave me some insights. Also, I recommend you do things to tighten down your security such as add an .htaccess or NGINX rule that only allows certain IP addresses or require certain headers to reach anything in your /service folder, and /service/* subfolders either over HTTP or HTTPS.
public static function leadExistsByPhone($sHomePhone,$sMobilePhone) {
$sHomePhone = (empty($sHomePhone)) ? 'xxxxxinvalid' : $sHomePhone;
$sMobilePhone = (empty($sMobilePhone)) ? 'xxxxxinvalid' : $sMobilePhone;
$asCheck = array (
'method' => 'get_entry_list',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => json_encode(array(
'session' => self::$SessionID,
'module_name' => 'Leads',
'query' => "
leads.phone_home = '$sHomePhone'
OR leads.phone_mobile = '$sMobilePhone'
",
'order_by' => 'leads.date_entered DESC',
'offset' => '0',
'select_fields' => array(),
'link_name_to_fields_array' => array(),
'max_results' => 999999,
'deleted' => false
))
);
$a = self::sendJSON($asCheck);
$nCount = # $a->result_count;
$nCount = intval($nCount);
return ($nCount > 0);
}
public static function leadExistsByEmail($sEmail) {
if (!filter_var($sEmail, FILTER_VALIDATE_EMAIL)) {
die('DENIED: invalid email address format');
}
$asCheck = array (
'method' => 'get_entry_list',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => json_encode(array(
'session' => self::$SessionID,
'module_name' => 'Leads',
'query' => "
leads.id IN
(
SELECT email_addr_bean_rel.bean_id
FROM email_addr_bean_rel
JOIN email_addresses
ON email_addr_bean_rel.email_address_id = email_addresses.id
WHERE
email_addresses.email_address = '$sEmail'
)
",
'order_by' => 'leads.date_entered DESC',
'offset' => '0',
'select_fields' => array(),
'link_name_to_fields_array' => array(),
'max_results' => 999999,
'deleted' => false
))
);
$a = self::sendJSON($asCheck);
$nCount = # $a->result_count;
$nCount = intval($nCount);
return ($nCount > 0);
}
I've enabled push notifications in my app, added the build hints, registered the api on the play developer console, created and loaded the apple certificates on my server. When I test the app on a device it successfully registers for push notifications. However my issue comes in with trying to actually send a push notification. I want it to send through PHP. I use this code which is taken straight from the developer guide. However this does not work... Is it a problem with my code or did I do something wrong in the enabling push notifications process.
<?php
include("config.php");
$args = http_build_query(array( 'certPassword' => 'XXXXXXXX', 'cert'
=>
'http://kyven.co.za/mibrand/certificate/XXXX.p12',
'production' => false,
'device' => null, 'packageName' => 'za.co.bonyelo.mibrand', 'email'
=>
'kyri88#gmail.com', 'type' => 1,
'auth' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
'body' => 'Test'));
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content'
=> $args
) );
$context = stream_context_create($opts);
$response = file_get_contents("https://codename-
one.appspot.com/sendPushMessage", false, $context);
die(json_encode($response));
?>
Got it. This is the code I used
<?php
include("config.php");
$args = http_build_query(array('token' => 'XXXXXXXXXXXXXXXXXXX',
'certPassword' => 'XXXXXXXX', 'cert' =>
'http://XXXXXXX/XXXXX/XXXXX/Certificates.p12',
'production' => false,
'device' => 'cn1-ios-XXXXXXXXXXXXXXXXXXXXXXXX',
'packageName' => 'za.co.bonyelo.mibrand', 'email' =>
'kyri88#gmail.com', 'type' => 1,
'auth' => 'XXXXXXXXXXX',
'body' => 'EAT MY BALLS'));
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $args
) );
$context = stream_context_create($opts);
$response =
file_get_contents("https://push.codenameone.com/push/push", false,
$context);
die(json_encode($response));
?>
I get quite annoyed by logging in to SO sometimes and i'd like to do it by just calling a PHP file. I tried doing so by sending a post request like this:
<?php
$url = 'https://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2f';
$data = array('email' => 'mymail#gmail.com', 'password' => 'mypasswort');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>
I also tried including more values like ssrc and fkey but i just shows the SO mainpage after submitting. It even stays at localhost (or wherever the script is running). If i enter a wrong password it marks it as incorrect - so it has to work at lest in some way (the data verification)...
You must send post data:
isSignup:false
isLogin:true
isPassword:false
isAddLogin:false
hasCaptcha:false
fkey:922b6dc5a5a375283c44e298246d7763
ssrc:head
email:asd
password:
submitbutton:Log in
oauthversion:
oauthserver:
openidusername:
openididentifier:
You should send first GET request and parse fkey from result page (this is CSRF protection).
fkey you should send with POST request with post data which I show you.
Simple example:
<?php
$url = 'https://stackoverflow.com/users/login?ssrc=head';
$data = array(
'email' => 'mail#email.z',
'password' => 'pass',
'isSignup' => false,
'isLogin' => true,
'isPassword' => false,
'isAddLogin' => false,
'hasCaptcha' => false,
'fkey' => '',
'ssrc' => 'head',
'submitbutton' => 'Log in',
'oauthversion' => '',
'oauthserver' => '',
'openidusername' => '',
'openididentifier' => ''
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'GET',
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
preg_match('~name\=\"fkey\"\svalue=\"([\w]+)\"~', $result, $matches);
$data['fkey'] = $matches[1];
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
But I recommend for you use SO API and you willn't have any problems
If you will have problem, write
drupal_http_request send "Your client has issued a malformed or illegal request" when spaces in url:
$uri = "https://www.googleapis.com/freebase/v1/search?query=&prefixed=true&limit=10&output=(type )&lang=en";
$options = array(
'method' => 'GET',
'timeout' => 3,
'headers' => array(
'Accept' => 'application/json',
),
);
$result = drupal_http_request($uri, $options);
" &output=(type ) " there is space after type . if i remove space then its working fine. but space give 400 bad request error. why?
The 'proper' Drupal way:
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => $search_string,
'output' => '(description owner)',
'filter' => '(any type:product type:brand)',
'limit' => YILD_FREEBASE_MAX_HITS,
'prefixed' => 'true',
'lang' => $lang ,
);
$uri = url($serivce_url, array('query' => $params, 'external' => TRUE));
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => $search_string,
'output' => '(description owner)',
'filter' => '(any type:product type:brand)',
'limit' => YILD_FREEBASE_MAX_HITS,
'prefixed' => 'true',
'lang' => $lang ,
);
$uri = $service_url . '?' . http_build_query($params);
This solve my problem .
I need to create a POST request with http_build_query. Following are my codes:
$uri_args = array (
'name' => 'Jack',
'surname' => 'Jackson',
'username' => 'jackson12',
'email' => 'Jack#mail.com',
);
$uri = http_build_query($uri_args);
header("Location: http://samplesite.com?$uri");
At the moment it generates a request like GET, but I need POST.
Consider that I do not want to use curl, ... only http_build_query.
<?php
$data = array(
'name' => 'Jack',
'surname' => 'Jackson',
'username' => 'jackson12',
'email' => 'Jack#mail.com',
);
$query = http_build_query($data);
// create context
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded' . PHP_EOL,
'content' => $query,
),
));
// send request and collect data
$response = file_get_contents(
$target = "http://example.com/target.php",
$use_include_path = false,
$context);
//some actions with $response
//...
// redirect
header("Location: http://samplesite.com");