I want to create product in shopify through api.I tried with below code,but its not working.
<?php
$products_array = array(
"product" => array(
"title" => "Test Product",
"body_html" => "<strong>Description!</strong>",
"vendor" => "DC",
"product_type" => "Test",
"published" => true ,
"variants" => array(
array(
"sku" => "t_009",
"price" => 20.00,
"grams" => 200,
"taxable" => false,
)
)
)
);
$SHOPIFY_API = "https://apikey:password#domainname.myshopify.com/admin/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $SHOPIFY_API);
$headers = array( "Authorization: Basic ".base64_encode("apikey:password"),
"Content-Type: application/json",
"charset: utf-8");
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
echo "</pre>";
?>
It gives response as '{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}'.any idea?
Please check if there is write permission is set or not for products in your private app.
I have found the issue, it's not about permission, is about the URL of the API you are posting it wrong URL. Here is the right one:
https://{apikey}:{password}#{hostname}/admin/api/{version}/{resource}.json
And here is the code:
<?php
$products_array = array(
"product" => array(
"title" => "New Test Product",
"body_html" => "<strong>Description!</strong>",
"vendor" => "DC",
"product_type" => "Test",
"published" => true ,
"variants" => array(
array(
"sku" => "t_009",
"price" => 20.00,
"grams" => 200,
"taxable" => false,
)
)
)
);
$API_KEY = 'apikey';
$PASSWORD = 'password';
$SHOP_URL = 'domainname.myshopify.com';
$SHOPIFY_API = "https://$API_KEY:$PASSWORD#$SHOP_URL/admin/api/2020-04/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $SHOPIFY_API);
$headers = array(
"Authorization: Basic ".base64_encode("$API_KEY:$PASSWORD"),
"Content-Type: application/json",
"charset: utf-8"
);
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
echo "</pre>";
?>
try "published" => false ,
Related
I'm working with OpenSRS EMAIL API. I'm trying to create a new user email for a domain. In their first example, they are using JSON format :
{
"credentials": {
"user": "domain_admin#example.com",
"password": "sw0rdf1sh"
},
"user": "bhayden#example.com",
"attributes": {
"name": "Bob Hayden",
"password": "changeit",
"delivery_forward": true,
"forward_recipients": [
"bob.hayden#example.com
}
}
I'm sending this with cURL
$json = array(
"user" => $emailName,
"attributes" => array(
"name" => "Janet User",
"password" => $email_password,
"delivery_forward" => false
)
);
//
//
$payload = json_encode( $json );
//
$data = [
'Content-Type:application/json',
'X-Username:' . $connection_details['reseller_username'],
'X-Signature:' . md5(md5($payload . $connection_details['api_key']) . $connection_details['api_key']),
];
//
$ch = curl_init($connection_details['api_host_port']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
//
$response = curl_exec($ch);
//
//header('Content-type: application/json');
//
echo '<pre>';
echo $response;
echo '</pre>';
This print following error
0.9 400 0 Invalid Content-Type XCP
What am I doing wrong?
I fixed this issue by reformarting the entire request. This worked for me
$data = array(
"user" => $emailName,
"attributes" => array(
"name" => "Janet User",
"password" => $email_password,
"delivery_forward" => false
)
);
$postdata = json_encode($data);
//
$ch = curl_init($connection_details_email['api_host_port']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
I am using a CURL post to an existing API. The API returns the error content must be JSON or plain text. I json_encode data and post the encoded data to the API.
In the API, it says the Content-Type should be application/x-www-form-urlencoded'.
However, it still returns the error content must be JSON or plain text. What could be the issue with my code below after submitting my data in JSON format?
This is a sample request body in the API doc.
"item" : "Store",
"content" : {
"channel":"false",
"hop": "false",
"msg": "Order successfully placed."
}
Controller
public function postUrl()
{
$url = "https://api.com/";
$data = array("item" => "Nike Shoes","content" => array ("channel" => "false" ,"hop" => "false","msg" => "Item Sold"));
$postdata = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);
Log::info($postdata);
}
Perhaps this is because boolean values are specified as strings.
public function postUrl()
{
$url = 'https://api.com/';
$data = [
'item' => 'Nike Shoes',
'content' => [
'channel' => false,
'hop' => false,
'msg' => 'Item Sold'
]
];
$postData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
curl_close($ch);
Log::info($postData);
}
Also try this
public function postUrl()
{
$url = 'https://api.com/';
$data = [
'item' => 'Nike Shoes',
'content' => [
'channel' => 'false',
'hop' => 'false',
'msg' => 'Item Sold'
]
];
$postData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/json']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
curl_close($ch);
Log::info($postData);
}
I am using ci framework, and i try to create notification using
onesignal API. I have hosted the website using https. But why my JSON
received is always false using onesignal API, but if i test using
postman it runs fine without error.
This is my code
function send_message($id, $nama) {
$content = array(
"en" => $nama
);
$heading =array(
"en" => $id
);
$fields = array(
'app_id' => "2cd5ad24-a2d9-4e7d-ac66-7494cebd8e84",
'included_segments' => array(
'Active Users'
),
'contents' => $content,
'headings' => $heading,
'url' => "https://esop.matahariled.com"
);
$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 MDQ0NjY0ZmYtZjQ2Yi00ODVmLTkzZjgtZmVkZDBkODk0MDFl'
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_CAINFO, $_SERVER['DOCUMENT_ROOT']."/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
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;
I'm having trouble just sending my first API call to PayPal.
I'm hoping its just simple syntax that I got wrong.
The error I'm getting:
"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
$sAccessToken = $json->access_token;
$sTokenType = $json->token_type;
$sAppID = $json->app_id;
print_r($json);
}
curl_close($ch);
$ch2 = curl_init();
//curl -v https://api.sandbox.paypal.com/v1/payments/payment
curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
//-H "Content-Type:application/json" \
//-H "Authorization: Bearer <Access-Token>" \
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
"Content-Type:application/json",
"Authorization: Bearer ".$sAccessToken,
));
$payer = array(
'payment_method' => 'credit_card',
'funding_instruments' => array(
'credit_card' => array(
'number' => "4417119669820331",
"type" => "visa",
"expire_month" => "11",
"expire_year" => "2018",
"cvv2" => "874",
"first_name" => "Betsy",
"last_name" => "Buyer",
"billing_address" => array(
"line1" => "111 First Street",
"city" => "Saratoga",
"state" => "CA",
"postal_code" => "95070",
"country_code" => "US"
),
),
),
);
$params = array(
'intent' => 'sale',
'payer' => $payer,
'transactions' => array(array(
"amount" =>"7.47",
"currency" =>"USD",
"details" => array(
"subtotal"=>"7.41",
"tax"=>"0.03",
"shipping"=>"0.03"
),
"description" => "This is the payment transaction description."
)
));
curl_setopt($ch2, CURLOPT_POST, true );
curl_setopt($ch2, CURLOPT_POSTFIELDS, json_encode( $params ) );
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true );
$response2 = curl_exec( $ch2 );
$errrors2 = curl_error($ch2);
print_r($response2." / ");
print_r($errrors2);
curl_close($ch2);