php how to send this data threw curl - php

I have to send this data threw curl:
-d '{"payer": {
"default_payment_instrument":"BANK_ACCOUNT",
"allowed_payment_instruments":["BANK_ACCOUNT"],
"default_swift":"FIOBCZPP",
"contact":{"first_name":"First",
"last_name":"Last",
"email":"first.last#example.com"
}
},
}'
How am I supposed to save those data into fields variable?
$fields = {
"payer": {
"default_payment_instrument":"BANK_ACCOUNT",
"allowed_payment_instruments":["BANK_ACCOUNT"],
"default_swift":"FIOBCZPP",
"contact":{"first_name":"First",
"last_name":"Last",
"email":"first.last#example.com"
}
},
};
$field_string = http_build_query($fields);
curl_setopt($process, CURLOPT_POSTFIELDS, $field_string);

This is GoPay right?
Do something like this:
$fields = [
"payer" => [
"default_payment_instrument" => "BANK_ACCOUNT",
"allowed_payment_instruments" => ["BANK_ACCOUNT"],
"default_swift" => "FIOBCZPP",
"contact" => [
"first_name" => "First",
"last_name" => "Last",
"email" => "first.last#example.com"
]
]
];
$json = json_encode($fields);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

Ok, posting stuff with cURL. Here you go...
<?php
$target_url = "http://domain.dev/post-acceptor.php";
$data_to_post = array(
"payer" => array(
"default_payment_instrument" => "BANK_ACCOUNT",
"allowed_payment_instruments" => "BANK_ACCOUNT",
"default_swift" => "FIOBCZPP",
"contact" => array(
"first_name" => "First",
"last_name" => "Last",
"email" => "first.last#example.com"
)
)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target_url);
curl_setopt($curl, CURLOPT_POST, count($data_to_post));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data_to_post));
$result = curl_exec($curl);
curl_close($curl);
Notes:
you might try to turn your JSON into an PHP array by using json_decode()

Related

add new product in shopify via php curl

if(isset($_POST['add_shopify']))
{
$title = $_POST['title'];
$body = $_POST['body_html'];
$vendor = $_POST['vendor'];
$type = $_POST['product_type'];
$price = $_POST['price'];
$images = $_POST['images'];
$product = array(
'title'=> $title,
'body_html' => $body,
'vendor'=> $vendor,
'product_type'=> $type,
"variants"=>[[
"price"=> $price
]],
"images" => [
[
"src"=> $images
]
]
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL," https://9e54fc.myshopify.com/admin/api/2022-10/products.json?access_token=*********************" );
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTPHEADER,array(
'Content-Type' => 'application/json',
));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($product));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
}
curl_close($curl);
if (isset($error_msg)) {
echo $error_msg;
}
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
print_r(json_decode($resp));
when i run this code it's return true. but when i am checking admin panel there are no product showing of that name. this code is working properly in postman api. please tell me about it what's wrong with it?
You are missing the "product" key in the $product array, please check the below code that there is a "product" key, and inside that, all the product details are there. You have to pass the data in JSON format, but you are using http_build_query, which Shopify doesn't accept with application/json header, so please use the below code to create a product via PHP curl.
<?php
$productData = [
"product" => [
"title" => "Burton Custom Freestyle 151",
"body_html" => "<strong>Good snowboard!</strong>",
"vendor" => "Burton",
"product_type" => "Snowboard",
"variants" => [
[
"option1" => "Blue",
"option2" => "155"
],
[
"option1" => "Black",
"option2" => "159"
]
],
"options" => [
[
"name" => "Color",
"values" => [
"Blue",
"Black"
]
],
[
"name" => "Size",
"values" => [
"155",
"159"
]
]
]
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://your-development-store.myshopify.com/admin/api/2022-10/products.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-Shopify-Access-Token' => '{access_token}',
'Content-Type' => 'application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($productData));
$response = curl_exec($ch);
curl_close($ch);

API JSON output Format

I have this code
<?php
$id = $_POST['id'];
$nume = $_POST['nume'];
$rrp = $_POST['rrp'];
$pret = $_POST['pret'];
$stoc = $_POST['stoc'];
$url = $_POST['url'];
$url1 = $_POST['url1'];
$url2 = $_POST['url2'];
$url3 = $_POST['url3'];
$username = "xx#xx.eu";
$password = "aaa";
$apiUrl = "https://partners.services.aaaa.eu/v1/mkpApi/product/save";
$auth = base64_encode($username . ":" . $password);
$headers = [
"Authorization: Basic " . $auth,
"Content-Type: application/x-www-form-urlencoded",
];
$payload[] = [
"id" => $id,
"locale" => "RO",
"hidden" => 0,
"currency" => "RON",
"brand" => "Decorepublic",
"name" => $nume,
"category_id" => 10008,
"status" => "1",
"vat" => "0",
"stock" => [[
"warehouse_id" => 1,
"value" => $stoc,
]],
"sale_price" => $pret,
"rrp" => $rrp,
"description" => $nume,
"images" => [
["url" => $url],
["url" => $url1],
["url" => $url2],
["url" => $url3],
],
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(["data" => json_encode($payload)]));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$response = json_decode($result, true);
print_r($response);
//echo '<br><br>';
echo json_encode($payload);
print $stoc;
?>
At output i have
status":"1","vat":"0","stock":[{"warehouse_id":1,"value":"200"}]
I need to have value without "" like this:
status":"1","vat":"0","stock":[{"warehouse_id":1,"value":200}],"
I trie to use print $stoc, but it returns value 1.
I tried get function but it doesn't work. Any idea?
If you've got a string in $stoc and you need to convert it to an integer before you encode it as JSON, then you can use intval.
"value" => intval($stoc)
Demo: http://sandbox.onlinephpfunctions.com/code/8b1ed16c28bfbc47f501981f2bf03f7b1bb9ce45

Date and status submit null values via curl in PHP

I have a following request method below which I am posting to an API via Curl
{
"status": [
{
"status": "string",
"date": "string"
}
],
"first_name": "string",
"last_name": "string"
}
The first_name and last_name values get posted successfully but status and date parameters submitted empty values. How do I also get the value of status and date parameters to be submitted also?
Here is the code:
<?php
$tok ='my token goes here';
$params= array(
'first_name' => "nancy",
'last_name' => "moree",
'status' => 'active',
'date' => '2020-12-29'
);
$url ='https://app.drchrono.com/api';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $tok"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
echo $output;
As described by the JSON requirement:
"status": [
{
"status": "string",
"date": "string"
}
]
Your actual status need to be an object inside another array key status:
$params= array(
'first_name' => "nancy",
'last_name' => "moree",
'status' => array(
array('status' => 'active', 'date' => '2020-12-29')
)
);
The array you're POSTing does not match the format you said you wanted to use. Try:
$params = [
'status' => [
'status': "active",
"date": "2020-12-29"
],
'first_name' => "nancy",
'last_name' => "moree",
];

Paymaya integration in PHP

im trying to create a customer in paymaya using curl in php.
im following this documentation http://developers.paymaya.com.payment-vault.s3-website-ap-southeast-1.amazonaws.com/#card-vault-customers-post
but its not returning the right response(it returns nothing)
<?php
require_once(DIR_VENDOR . 'PayMaya-PHP-SDK-master/sample/autoload.php');
Class Paymaya {
public function paymayaInit(){
PayMayaSDK::getInstance()->initCheckout("pk-nRO7clSfJrojuRmShqRbihKPLdGeCnb9wiIWF8meJE9", "sk-jZK0i8yZ30ph8xQSWlNsF9AMWfGOd3BaxJjQ2CDCCZb", "SANDBOX");
}
public function createCustomer(){
$this->paymayaInit();
// $ch = curl_init("https://pg-sandbox.paymaya.com/payments/v1/customers");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pg-sandbox.paymaya.com/payments/v1/customers");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Basic c2stOWxSbUZUVjhCSWR4b1hXbTVsaURBbEtGMHlMNGdaendtRFFBbW52eFdPRjo="));
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$body = array(
"firstName" => "Ysa",
"middleName" => "Cruz",
"lastName" => "Santos",
"birthday" => "1987-10-10",
"sex" => "F",
"contact" => array(
"phone" => "+63(2)1234567890",
"email" => "ysadcsantos#gmail.com"
),
"billingAddress" => array(
"line1" => "9F Robinsons Cybergate 3",
"line2" => "Pioneer Street",
"city" => "Mandaluyong City",
"state" => "Metro Manila",
"zipCode" => "12345",
"countryCode" => "PH"
),
"metadata" => array()
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
$response = curl_exec($ch);
curl_close($ch);
// die(print_r($response));
return json_decode($response);
}
}
here is my class.
does anyone already tried integrating paymaya in php?
also i have to comment the namespace in PayMayaSDK.php to be able to use PayMayaSDK class
Thanks for your credentials posted in question I made few requests and found what was wrong.
First of all I used curl_error($ch) to find out what error was.
It's The requested URL returned error: 400 Bad Request.
Problem is that you set Content-Type to be json, but sending URL encoded query.
Change http_build_query to json_encode
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pg-sandbox.paymaya.com/payments/v1/customers");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Basic c2stOWxSbUZUVjhCSWR4b1hXbTVsaURBbEtGMHlMNGdaendtRFFBbW52eFdPRjo="));
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$body = array(
"firstName" => "Ysa",
"middleName" => "Cruz",
"lastName" => "Santos",
"birthday" => "1987-10-10",
"sex" => "F",
"contact" => array(
"phone" => "+63(2)1234567890",
"email" => "ysadcsantos#gmail.com"
),
"billingAddress" => array(
"line1" => "9F Robinsons Cybergate 3",
"line2" => "Pioneer Street",
"city" => "Mandaluyong City",
"state" => "Metro Manila",
"zipCode" => "12345",
"countryCode" => "PH"
),
"metadata" => array()
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
$response = curl_exec($ch);
echo __FILE__."#".__LINE__."<pre>";
var_dump($response, curl_error($ch));
echo "</pre>";
curl_close($ch);

PHP Curl - How to do POST request and the parameters should be in JSON format in the body

I need to replicate the same POST request in PHP curl. The request parameters should be in json in the body and the response too is a json object.
{
"api_key": "scTrCT",
"test": "true",
"service_provider_list": [
{
"facility_name": "ALL YOUR SMILE NEEDS DENTAL CENTERS",
"provider_name": "DRS. HERMAN AND MACK P.C",
"tax_id": "12345678
}
],
"payer_ids": [
"00431"
],
"transaction_type": "270",
"effective_date": "2014-01-12"
}
Please try this:
<?php
$json = array(
"api_key" => "scTrCT",
"test" => "true",
"service_provider_list" => array(
"facility_name" => "ALL YOUR SMILE NEEDS DENTAL CENTERS",
"provider_name" => "DRS. HERMAN AND MACK P.C",
"tax_id" => "12345678"
),
"payer_ids" => array(
"00431"
),
"transaction_type" => "270",
"effective_date" => "2014-01-12"
);
$json = json_encode($json);
$ch = curl_init('http://gds.eligibleapi.com/v1.3/enrollment.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

Categories