How to use cURL in HTML? - php

Probably a stupid question but how on earth does one use cURL within HTML?
I'm trying to set up a Coinbase API and I'm getting error instantly, Is cURL only available in certain websites, React etc? Or can this be used in standard HTML/PHP?
Obviously the first error I'm getting is "unexpected POST".
Coinbase instructions aren't very clear to someone whos never used React or cURL before.
I would be extremely grateful if someone could just explain it a little clearer :)
My undertanding here is that once the "Pay with Crypto" submit button has been clicked I'm assuming this "data.JSON" file is included with the POST info? and it should submit the details to a hosted checkout page, including the .json details/price, right? This might be wrong but I feel I'm on the right track.
This is the code in my html page...
<?php
curl -X POST https://api.commerce.coinbase.com/charges/ \
-H "Content-Type: application/json" \
-H "X-CC-Api-Key: MY_API_KEY" \
-H "X-CC-Version: 2018-03-22" \
-d "#data.json"
?>
Here I have my "data.JSON" file with this code...
{
"name":"TZ",
"description":"Selected Repair Service",
"pricing_type":"<?php echo $fetch['price']; ?>"
}

Related

How to Post data from external HTML form to Eloqua Form using cURL

I want to post data from an external HTML form to a simple form created in Eloqua, using cURL method. I tried to follow this documentation but I am not able to post data to Eloqua.
When I try from command line (Windows) -
curl --user "usercreds" --header "Content-Type: application/json" --request POST --data "{"testfirstname":"abc","testlastname":"def","singleCheckbox":1}" https://secure.p0{POD Number}.eloqua.com/api/REST/1.0/data/form/{formid}
I get below error:
[{"type":"ObjectValidationError","property":"fieldValues","requirement":{"type":"NoDuplicatesRequirement"},"value":""}]
When I try from PHP, as mentioned here https://www.eehelp.com/question/using-curl-to-repost-to-eloqua-data/
or https://github.com/fredsakr/eloqua-php-request the curl returns HTTP code 0.
This is a simple form created in Eloqua without any validations.
I do not know what I am doing wrong here.

cURL and PHP post request

I am new at cURL and can`t understand. I have for example this code
curl -d '
{
"data": {
"title": "Bubble Nebula",
"body": "It`s found today at 21:00",
"icon": "https://peter-gribanov.github.io/serviceworker/Bubble-Nebula.jpg",
"image": "https://peter-gribanov.github.io/serviceworker/Bubble-Nebula_big.jpg",
"click_action": "https://www.nasa.gov/feature/goddard/2016/hubble-sees-a-star-inflating-a-giant-bubble"
}
"to": "YOUR-TOKEN-ID"
}' \
-H "Content-Type: application/json" \
-H "Authorization: key=AAAAaGQ_q2M:APA91bGCEOduj8HM6gP24w2LEnesqM2zkL_qx2PJUSBjjeGSdJhCrDoJf_WbT7wpQZrynHlESAoZ1VHX9Nro6W_tqpJ3Aw-A292SVe_4Ho7tJQCQxSezDCoJsnqXjoaouMYIwr34vZTs" \
-X POST "https://fcm.googleapis.com/fcm/send"
How should i do it work well with php?
Most of the time I just use file_get_contents() for my requests, but that trick is sadly only for GET requests.
However, I recently discovered a new trick that can be used with Post and Get requests(Including everything else that curl can) and still avoids the horrible php library phpcURL, And the best part is, that you don't even need to change your curl command. Try the code below.
<?php
$curl = "your curl command" // paste your curl command here
exec("$curl > curloutput.txt");
$response = file_get_contents("curloutput.txt");
echo($response);
?>
This code should execute your given curl command and return the output. Yes, I know this sounds like a really janky method but it works every time bugless and it is super easy to use.

How to get web hook data with GitHub using PHP

I've setup a GitHub webhook with a secret and selected application/x-www-form-urlencoded for the content type.
I tried to search around but there does not seem to be much info on how to use the post data, such as what does GitHub include in the POST request, can I just do:
$_POST["secret"] or is there more to it than that? I know I could test this myself but with the way I setup the webhook its hard to see the output and more of a pain to do a var_dump() for POST.
So basically my question is what is the POST layout when GitHub sends a POST request, because I am looking to validate which branch was pushed and validate the secret as well.
seems it is translated by PHP as $_SERVER['HTTP_X_HUB_SIGNATURE']
but when in doubt as to how PHP will name a header, set up a page with <?php phpinfo(~0); , and run curl with the url like curl http://ratma.net/phpinfo.php --header "X-Hub-Signature: test" -v 2>&1 | grep -i test , and you should see what the header is called. in this case, i got
<tr><td class="e">$_SERVER['HTTP_X_HUB_SIGNATURE']</td><td class="v">test</td></tr>

PerfectCURL - How to make the following call?

I am using PerfectSwift for a RESTful API to bridge our TeamCity build server and HipChat; however I am stuck at a point whereby I am unable to post to the HipChat backend using Perfect's cURL wrapper.
The command I am trying to mimic is:
curl -d '{"color":"green","message":"My first notification (yey)","notify":false,"message_format":"text"}' -H 'Content-Type: application/json' https://<MY DOMAIN>/v2/room/509/notification?auth_token=<MY AUTH TOKEN>
I currently have the following code in my Perfect program:
let curl = CURL(url: "https://<MY DOMAIN>/v2/room/509/notification?auth_token=<MY AUTH TOKEN>")
curl.setOption(CURLOPT_POST, int: 1)
curl.setOption(CURLOPT_POSTFIELDS, s: "{\"color\":\"green\",\"message\":\"My first notification (yey)\",\"notify\":false,\"message_format\":\"text\"}")
curl.setOption(CURLOPT_HTTPHEADER, s: "[Content-Type:application/json]")
curl.perform { (code, header, body) in
}
However, the message never gets to HipChat, or, if it does, it's not in a readable format.
When I paste the first command into terminal, everything works as expected.
From my understanding, this uses a similar system to PHP, and I am therefore including the PHP tag as I feel PHP developers may be able to offer advice if I am using the wrong CURLOPTs etc...
Thanks in advance.

API query/result (json) in php

Let me preface this by stating that I'm a complete novice when it comes to php, but I have a need to create a php script for a particular curl request to monitor the state of a service.
The curl command is:
curl --insecure --digest 'https://admin:password#localhost:9993/management' --header "Content-Type: application/json" -d '{"operation":"read-attribute","name":"server-state","json.pretty":1}'
And it returns this output:
"outcome" : "success",
"result" : "running"
I've searched around, found some examples and hacked together a few different scripts but none of them give me the desired result (entirely my fault). I seem to have the authentication side working but I'm failing miserably at sending the array and returning a result. Essentially, I'm interested in getting the "running" field back (or whatever other value it might return) which I can use in out monitoring system. The closest I've got is for the server to throw a java ioexception at me because of an invalid character.
Any assistance will be greatly appreciated.
Thanks,
Mark J.
Never mind. I needed to json_encode my data string. All working now.

Categories