Posting to Shopify using PHP - php

I'm trying to post from a PHP page to Shopify to indicate that an order item has been fulfilled. I must not be doing this correctly because I keep getting a null result. I'm doing this on a test order that was cancelled, but I would expect that I'd at least get some sort of non-null response if that were the problem. Here is my code:
$API_KEY = 'my-api-key';
$SECRET = 'my-shared-secret';
$PASS = 'my-shopify-password';
$STORE_URL = 'mydomain.myshopify.com';
$ITEM_ID = 'my-item-id';
$ORDER_ID = 'my-order-number';
$TRACKING_NUMBER = '12345';
$TRACKING_URL = 'http:\/\/test.com\/testurl';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'#'.$STORE_URL.
'/admin/orders/'.$ORDER_ID.'/fulfillments.json';
$data = array('fulfillment' =>
array(
'tracking_number' => $TRACKING_NUMBER,
'tracking_company' => 'USPS',
'tracking_url' => $TRACKING_URL,
'line_items' =>
array(
'id'=>$ITEM_ID,
),
),
);
$session = curl_init( $baseUrl );
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
var_dump($json);

You are sending a json in the POST section, are you sure this is what you really want to do? The post data should be more like this, sending data1=test1&data2=test2. source
<?php
$API_KEY = 'my-api-key';
$SECRET = 'my-shared-secret';
$PASS = 'my-shopify-password';
$STORE_URL = 'mydomain.myshopify.com';
$ITEM_ID = 'my-item-id';
$ORDER_ID = 'my-order-number';
$TRACKING_NUMBER = '12345';
$TRACKING_URL = 'http:\/\/test.com\/testurl';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'#'.$STORE_URL.'/admin/orders/'.$ORDER_ID.'/fulfillments.json';
$fields = array(
'lname' => urlencode($last_name),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session, CURLOPT_URL, $baseUrl);
curl_setopt($session, CURLOPT_POST, count($fields));
curl_setopt($session, CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
var_dump($result);
//close connection
curl_close($ch);
?>
If you really wants to send JSON via the POST fields, maybe you are missing the length of your data : source
curl_setopt($, , CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);

Shouldn't you be using complete.json instead ??? I may be misunderstanding but it sounds like your trying to complete a pending fulfillment.
/admin/orders/#{id}/fulfillments/#{id}/complete.json
https://docs.shopify.com/api/fulfillment#complete

I found the problem. Shopify requires the line items to have an additional array, so instead of this:
'line_items' =>
array(
'id'=>$ITEM_ID,
)
It should be this:
'line_items' =>
array(
array(
'id'=>$ITEM_ID,
)
)

Related

Onesignal notification api stopped sending when added Huawei

I have php api for Onesingal notifications to send notification to users when I approve post on my app, it was working perfectly until I added new platform on Onesingal Dashboard which is Huawei, then when I tried to send notification from the app it does't send!
I noticed in the Dashboard that Huawei requires title to send notification! and title already on my api ! but I cann't figure it out!
Notification image
<?php
$title = $_REQUEST["title"];
$sendnotification = $_REQUEST["sendall"];
//here it sends to the post owner that his post is approved
//if ($_SERVER['REQUEST_METHOD'] == 'POST'){
if($isApproved=="1")
{
$ph=$phone;
$arr = $mysqli->query("select * from users where phone='$ph'")->fetch_array(MYSQLI_ASSOC);
$player_id = $arr["token"];
if(!empty($player_id)) {
$content = array(
"en" => "Your post is approved"
);
$heading = array(
"en" => "Your post is approved"
);
$fields = array(
'app_id' => "xxxxxx-xxxxxx-xxxx-xxxx-xxxxxxxxx",
'include_player_ids' => array($player_id),
'contents' => $content
);
$fields = json_encode($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 xxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
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);
$one2 = curl_exec($ch);
curl_close($ch);
}
}
// here it sends notification fo all users with the post title
if ($isApproved=="1"&& $sendnotification=="1")
{
$content = array(
"en" => $title
);
$heading = array(
"en" => $title
);
$fields = array(
'app_id' => "xxxxxx-xxxxxx-xxxx-xxxx-xxxxxxxxx",
'included_segments' => array('All'),
'contents' => $content
);
$fields = json_encode($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 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
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);
$one2 = curl_exec($ch);
curl_close($ch);
echo $one2;
}
//}
echo json_encode($se);
?>

Adding OneSignal segments to filters array

I am using following PHP script to send OneSignal push notifications to subscribed devices.
<?PHP
function sendMessage(){
$content = array(
"en" => 'Testing Message desde el backend small icon '
);
$fields = array(
'app_id' => "xxxx",
'filters' => array(array("field" => "tag", "key" => "correo", "relation" => "=", "value" => "xxx")),
'data' => array("foo" => "bar"),
'small_icon' =>"ic_push",
'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 xxxxx'));
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");
?>
This script is working fine, and the notifications are sent.
Now I need to add more filters, for example I need to add a filter to two other segments that I have created at the OneSignal dashboard.
The segments are "skateboard" and "administrators".
How can I add these two segment to the filters array?
You need to pass segments name array in fields like you are passing tags.
$content = array(
"en" => 'Notification Message Here..'
);
$heading = array(
"en" => 'Heading goes here..'
);
$fields = array(
'app_id' => 'XXXXXXXXXXXXXXXXXXXXX',
'contents' => $content,
'headings' => $heading,
'included_segments' => array('SegmentName1','SegmentName2'),
'tags' = array(array("key" => "state", "relation" => "=", "value" => 'Delhi'))
);
$fields = json_encode($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 XXXXXXXXXXXXXXXXXXX'));
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;

Not able to send push notifications in android even after getting success1 through my PHP script

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

Unable to insert post in blogger using PHP

I am using the following code to insert a post in blogger but the response I am receiving from Google is null. What am I doing wrong?
<?php
$key = "MyKey";
$blog_id = "123456";
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blog_id.'/posts/';
$postData = array(
'kind' => 'blogger#post',
'blog' => array('id' => $blog_id),
'title' => 'This is title',
'content' => 'This is content'
);
$data_string = json_encode($postData);
$head = array();
$head[] = 'Authorization: '.$key;
$head[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$rsp = curl_exec($ch);
$results = json_decode($rsp);
var_dump($results);
?>

Invalid Credentials error when trying to add post to blogger using API

I have trying to add a post to blogger using API since yesterday but no luck. Here is my code. When I run it, I get "Invalid Credentials" error.
I have also posted screenshot below which shows from where I am getting that key in Google Developer Console. May be I am picking up wrong key?
Code
<?php
$key = "J7c";
$blog_id = "123456";
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blog_id.'/posts/';
$postData = array(
'kind' => 'blogger#post',
'blog' => array('id' => $blog_id),
'title' => 'This is title',
'content' => 'This is content'
);
$data_string = json_encode($postData);
$head = array();
$head[] = 'Authorization: '.$key;
$head[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$rsp = curl_exec($ch);
$results = json_decode($rsp);
var_dump($rsp);
?>
Error
Google Developer Console

Categories