Is there any API for ether sent transaction in ethereum - ethereum

I want to do a transaction (sent ether or token) by API, not from web3.js
I have tried etherscan, block cypher
This is a get balance API code like this I want send transaction code
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.etherscan.io/api?module=account&action=balance&address=0xC1A71f1eFC01D77aA102A9CE248c5360C347Abc8&tag=latest&apikey=YourApiKeyToken",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
// Set Here Your Requesred Headers
'Content-Type: application/json',
),
));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

What you need to do is to make a RPC call to a ethereum node. Basically, web3 libraries are wrappers around this.
Docs can be found here : https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sendtransaction

Related

API Gravity form, problem with size of JSON

I have a problem with the Rest API of gravity forms, I use it with the cURL method, I have no problem with, except with the size of the JSON chain, he only give me 10 last entries to the concerned forms, if anyone knows how to ask more entry (or all the entries of the form, that would be perfect but some forms have more than 1000 entry, I think it's too heavy for the JSON link).
$CK =ck_12345;
$CS =cs_12345;
$curl = curl_init('https://yoursite.wpserveur.net/wp-json/gf/v2/forms/(form:id)/entries');
curl_setopt_array ($curl, [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERNAME => "$CK",
CURLOPT_PASSWORD => "$CS",
]);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$data = curl_exec($curl);
if ($data === false) {
var_dump(curl_error($curl));
}
else {
$data = json_decode($data, true);
}
curl_close($curl);

Not getting any json results from openstreet map using Nominatim and php file_get_contents() or Curl

I am trying to get results from a json file fetched from openstreetmap.org. When entering the url into the browser I see the json file beeing returned inside the browser. IfI try to read the json using a php script, then nothing happens. Not if I use file_get_contents, but also not if I use curl.
function geocode($address){
// url encode the address
$address = urlencode($address);
//Url openstreetmap
$url = "https://nominatim.openstreetmap.org/?addressdetails=1&q=$address&format=json&limit=1";
// Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
return json_decode($result, true);
}
And also if I use file_get_contents, there are no results:
function geocode($address){
// url encode the address
$address = urlencode($address);
$url = "http://nominatim.openstreetmap.org/search/?format=json&addressdetails=1&q={$address}&format=json&limit=1";
// get the json response
$resp_json = file_get_contents($url);
return json_decode($resp_json, true);
}
What am I possibly doing wrong?
I've changed your code according Nominatim Usage Policy.
In short, CURL is better method for you but have to add at least these HTTP request headers:
HTTP Referer
User-Agent
And it's also important to not send more than 1 request per second (read the Usage Policy link above).
I've changed your code so this should work:
<?php
function geocode($address){
$address = urlencode($address);
$url = "https://nominatim.openstreetmap.org/?addressdetails=1&q=$address&format=json&limit=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
echo "<pre>";
print_r(geocode("Time Square, New York City"));
Output:
Array
(
[0] => Array
(
[place_id] => 162597874
[licence] => Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright
[osm_type] => way
[osm_id] => 304980452
[boundingbox] => Array
(
[0] => 41.7382344
[1] => 41.7384706
[2] => -74.0371548
[3] => -74.0367398
)
[lat] => 41.73835325
[lon] => -74.03694730280651
[display_name] => Time Square, 652, NY 299, Elting Corners, Lloyd, Town of Lloyd, Ulster County, New York, 12561, United States
[class] => building
[type] => yes
[importance] => 0.401
[address] => Array
(
[building] => Time Square
[house_number] => 652
[road] => NY 299
[hamlet] => Elting Corners
[town] => Lloyd
[municipality] => Town of Lloyd
[county] => Ulster County
[state] => New York
[postcode] => 12561
[country] => United States
[country_code] => us
)
)
)

why do i get an error in this json request?

this is my function:
function transaction_create($wallet_id, $passphrase, $payment_address, $payment_amount, $metadata = array()) {
$api_host = "";
$api_user = "";
$api_pass = "";
$api_url = "/v2/";
$api_endpoint = "wallets/" . $wallet_id . "/transactions";
// post body
$transaction = array(
"passphrase" => $passphrase,
"payments" => array(
"address" => $payment_address,
"amount" => array(
"quantity" => $payment_amount,
"unit" => "lovelace"
),
),
"metadata" => $metadata,
"time_to_live" => array(
"quantity" => 10,
"unit" => "second"
)
);
$curl = curl_init('https://' . $api_user . ":". $api_pass . "#". $api_host . $api_url . $api_endpoint);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($transaction));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$api_response = curl_exec($curl);
curl_close($curl);
// $api_response - available data from the API request
return $api_response;
}
this is the docs about the api:
https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/postTransaction
the part that is giving me the problem is formatting the "payments" array. the results of my function are:
"{"code":"bad_request","message":"Error in $.payments: parsing
NonEmpty failed, expected Array, but encountered Object"}"
then if i create a class and make the "payments" into a real object, i get the opposite error, it wants an array not an object... ??!!?? im confused.
From the doc, Amount is an object, not an array - Also, so is Time to Live. PHP seems to have some gotcha's when it comes to JSON - my recommendation would be to try to capture the post as it is presented, and make sure arrays are [] and objects are {}.

How to post JSON formatted data to Shopify?

For example user from WooCommerce trigers woocommerce_add_to_cart hook is it possible to send product data to Shopify app as json and that data be accepted via Shopify app as carts/create webhook? If so how could I do this?
UPDATE:
Finally found a way of doing that. Here is and example code, for adding order:
$ch = curl_init("https://api_key:api_pass#shop_name.myshopify.com/admin/orders.json");
$order = array('order' => array(
'line_items' => array(
array(
'title' => 'Big Brown Bear Boots',
'price' => '74.99',
'grams' => '1300',
'quantity' => 3,
),
array(
'title' => 'Clicky Keyboard',
'price' => '150',
'quantity' => 1,
)
)
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($order));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$this->response = curl_exec($ch);
And you hook this code to woocommerce action.

How to structure JSON to be sent over PHP's cURL

I have an api which works fine when I use POSTMAN, but now that I am trying to send it with PHP I am getting an API error message in response.
Format Error: The exception message is 'The incoming message has an unexpected message format 'Raw'
Which I am sure is related to my data structure. can someone tell me where I am going wrong?
Header
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
JSON Body
{
"accessID": "ASASD22",
"password": "DASD2DQA",
"messages": [
{
"DestinationID": "22D2D2D22D",
"UserMessageID": 133,
"RawPayload": [1,31, 34, 43]
}
]
}
Here is the PHP code I wrote, but returns an error code; can you guys see any issues?
$headers= array('Connection: Keep-Alive','Content-Type: application/json; charset=utf-8','Connection: Keep-Alive');
$data = array(
"accessID" => "ASASD22",
"password" => "DASD2DQA",
"messages" => array(
"DestinationID" => "22D2D2D22D",
"UserMessageID" => "133",
"RawPayload" => "[1,31, 34, 43]"
)
);
$url_send ="http://api.SITE.com/RST-MESSAGE.svc/submit.json/";
$str_data = json_encode($data);
function sendPostData($url, $post){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo " " . sendPostData($url_send, $str_data);
It's difficult to guess, you should check the API's documentation.
But, if the data posted as "JSON Body" is the request you send when it works then you should replicate it exactly in PHP (which you don't):
$data = array(
"accessID" => "ASASD22",
"password" => "DASD2DQA",
"messages" => array(
array(
"DestinationID" => "22D2D2D22D",
"UserMessageID" => "133",
"RawPayload" => array(1, 31, 34, 43),
),
)
);
I did two changes to your data structure. See below the pieces of JSON body that ask for changes in the structure of $data:
"messages": [ { ... } ] - messages is an array of objects, not a single object;
"RawPayload": [1, 31, 34, 43] - RawPayload is an array of integers, not a string;
I found a way to correctly send the entire JSON with in the PHP cURL function
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.SITE.com/Messages.svc/submit.json/" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, '{
"accessID": "ASASD22",
"password": "DASD2DQA",
"messages": [
{
"DestinationID": "22D2D2D22D",
"UserMessageID": 133,
"RawPayload": [1,31, 34, 43]
}
]
}' );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json'));
$result=curl_exec ($ch);
echo $result;
?>
so basically I wrapped the entire JSON into curl_setopt($ch, CURLOPT_POSTFIELDS, $data) and send it that way!