Codename one push notifications with php issue - php

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));
?>

Related

Error in getting Active subscriber count from Sendy API

While trying to fetch the active subscriber count from Sendy API, I am getting following error
The POST request looks like this:
http://my-sandy-Installation/api/subscribers/active-subscriber-count.php
In header: Content-Type:application/x-www-form-urlencoded
In Body:
api_key= mykey
list_id= mylistid
Can anyone please help?
$boolean = 'true';
$postdata = http_build_query(
array(
'email' => $email,
'api_key'=>'your api_key',
'list' => 'Your List id',
'boolean' => 'true'
)
);
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
$context = stream_context_create($opts);
$result = file_get_contents($your_installation_url.'/subscribe', false, $context);

Automated login to Stackoverflow

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

Bigcommerce API - Creating Webhooks - Invalid Header

I'm making small steps into this project I am working on. Now creating and registering a webhook. I'm getting the below response:
400 - Invalid Header
I have tried the following code:
// Send a request to register a web hook
$http2 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
'X-Custom-Auth-Header' => $access_token,
)
)
));
$request = $http2->post('/'.$store_hash.'/v2/hooks', null, array(
'scope' => 'store/order/*',
'destination' => 'https://example.com/process_order.php',
'is_active' => true
));
$response = $request->send();
$body = $response->getBody(true);
var_dump($body);
echo '<p>Status Code: ' . $response->getStatusCode() . '</p>';
... and
// Send a request to register a web hook
$http2 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
)
)
));
$request = $http2->post('/'.$store_hash.'/v2/hooks', null, array(
'scope' => 'store/order/*',
'headers' => array(
'X-Custom-Auth-Header' => $access_token,
),
'destination' => 'https://example.com/process_order.php',
'is_active' => true
));
$response = $request->send();
$body = $response->getBody(true);
var_dump($body);
echo '<p>Status Code: ' . $response->getStatusCode() . '</p>';
I am working with the documentation here:
https://developer.bigcommerce.com/api/stores/v2/webhooks#create-a-hook
However, I can't seem to work out what {secret_auth_password} is as well? The documentation doesn't explain this. I am sending the Client ID and Client Header as part of the headers as well.
Still getting Invalid Header as a response.
I am using Guzzle.
Can anyone assist me on this please?
I have finally worked out what I did wrong after numerous attempts.
Answer: send the data in JSON format.
Resolved Code:
// Send a request to register a web hook
$http3 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
)
)
));
$request = $http3->post('/'.$store_hash.'/v2/hooks', null, json_encode(array(
'scope' => 'store/order/statusUpdated',
'destination' => 'https://example.com/process_order.php',
)));
$response = $request->send();

Wordpress HTTP API results in JSON_ERROR_SYNTAX

I'm Trying to correctly use wp_remote_get in my wordpress plugin which uses our API. The API checks the request body for is_json, which returns 4 - aka JSON_ERROR_SYNTAX. Using cURL has the desired response. Here's my code:
$args = array(
'body' => array('api_key' => 123),
'timeout' => '5',
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'cookies' => array()
);
$result = wp_remote_request('https://myapi.endpoint/api/1.0/request', $args);
var_dump($result['body'])
var_dump: string(13) ""api_key=123""
whereas the var_dump of my cURL request is string(15) "{"api_key=123"}"
Any ideas as to what I am doing incorrectly?
As Marc B stated, wp isn't sending json. You need to set the content type in the headers, and also send it as json:
$body = array('api_key' => 123);
$headers = array (
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Content-Length' => strlen(json_encode($body)
);
$args = array(
'method' => 'POST',
'headers' => $headers,
'body' => json_encode($body)
);

Updating a jira issue with the rest api. NOT soap

my php function to update jira issue is like this.i have hardcoded the issue id.it generates an error in if (property_exists($result, 'errors')). saying parameter is not an object.
function post_to($resource, $data) {
$jdata = json_encode($data);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_URL => JIRA_URL . '/rest/api/latest/' . $resource,
CURLOPT_USERPWD => USERNAME . ':' . PASSWORD,
CURLOPT_POSTFIELDS => $jdata,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_RETURNTRANSFER => true
));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
function create_issue($issue) {
return post_to('issue/10224/editmeta', $issue);
}
$new_issue = array(
'update' =>array(
'fields' => array(
'project' => array('key' => 'DOTNET'),
'summary' => 'Test via REST',
'description' => 'Description of issue goes here.',
'issuetype' => array('name' => 'Task')
))
);
$result = create_issue($new_issue);
if (property_exists($result, 'errors')) {
echo "Error(s) creating issue:\n";
var_dump($result);
}
}
what am i doing wrong here? please answer.
editmeta should be used only to OBTAIN meta data from an issue.
To update an issue you must use the issue method.
You can check the Jira API details here:
https://docs.atlassian.com/jira/REST/cloud/#api/2/
Not really sure, let's try some thing:
change
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
to:
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json'
);
and:
$new_issue = array(
'update' =>array(
'fields' => array(
'project' => array('key' => 'DOTNET'),
'summary' => 'Test via REST',
'description' => 'Description of issue goes here.',
'issuetype' => array('name' => 'Task')
))
);
to:
$new_issue = array(
'fields' => array(
'project' => array('key' => 'DOTNET'),
'summary' => 'Test via REST',
'description' => 'Description of issue goes here.',
'issuetype' => array('name' => 'Task')
)
);
lastly, change:
CURLOPT_URL => JIRA_URL . '/rest/api/latest/' . $resource,
to your real address, as well as writing 2 instead of latest, i.e.:
CURLOPT_URL=>'http://localhost/rest/api/2/issue/',
try this, and let me know how it's goes, good luck!
EDIT
try changing:
CURLOPT_POST => 1,
to:
CURL_POST=>true,
CURLOPT_VERBOSE=>1,
btw, where is your jira server? didn't you say it was hosted? localhost:8080 will work only if Jira is installed locally. If so, try opening it using a browser http://localhost:8084/rest/api/2/issue/
EDIT 2
Make sure the Allow Remote API Calls is turned ON under Administration > General Configuration.
to update an issue:
the URL should point to the soon-to-be-updated issue, i.e.:
http://localhost/rest/api/2/issue/TEST-31
and the data should be the same as before, meaning:
$new_issue = array(
'fields' => array(
'project' => array('key' => 'DOTNET'),
'summary' => 'Test via REST',
'description' => 'Description of issue goes here.',
'issuetype' => array('name' => 'Task')
)
);
just as you wrote when you tried to create an issue. you can find here some simple example of how to do so.
EDIT 3
Are you sure you have the right jira address? try again opening a browser and going to the URL and compare it to this example. If the page won't show, you will have to contact Jira's support and ask them how come you can't access the hosted Jira remote API.

Categories