I need to implement the following curl using PHP.
$ curl https://api.dev.strike.acinq.co/api/v1/charges \
-u sk_pJDwxFxCVw5fQJhRRMpf29jReUjjN: \
-X POST \
-d amount=42000 \
-d currency="btc" \
-d description="1%20Blockaccino"
This is what I have so far.
$post=array();
$post["amount"]=4200;
$post["currency"]="btc";
$post["description"]="1%20Blockaccino";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERNAME, "sk_pJDwxFxCVw5fQJhRRMpf29jReUjjN:");
curl_setopt($ch, CURLOPT_URL, "https://api.dev.strike.acinq.co/api/v1/charges");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$output = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http==200) {
} else {
}
It returns
{ "code" : 401, "message" : "authentication failed" }
Even though the CURLOPT_USERNAME is accurate. Are there other issues I am not seeing?
Thanks in advance.
CURLOPT_USERNAME was added in PHP 5.5.0.
If you have an older version then you may want to try:
curl_setopt($ch, CURLOPT_USERPWD, "sk_pJDwxFxCVw5fQJhRRMpf29jReUjjN:");
Here is the final WORKING solution...
$post=array();
$post["amount"]=100000;
$post["currency"]="btc";
$post["description"]="test";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERNAME, "sk_XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
curl_setopt($ch, CURLOPT_URL, "https://api.strike.acinq.co/api/v1/charges");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
$output = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http==200) {
} else {
}
Related
I have a curl request, which I can send from the cli like this:
curl -X 'PUT' \
'https://XXX/endpiontY' \
-H 'Authorization: Bearer <TOKEN>' \
-H 'Content-Type: application/json' \
-d '{
"value": "TestValue"
}'
(curl --version returns curl 7.81.0, so default is CURL_HTTP_VERSION_2TLS)
I am trying to send the same information with PHP, but I get
HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)
My Setup
$json = array('value' => 'TestValue');
$json_string = json_encode($json);
$headers = array(
'Content-Type:application/json',
'Content-Length: ' . strlen($json_string)
)
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($ch, CURLOPT_PUT, true);
$result = curl_exec($ch);
if ($result === false) {
$error = curl_error($ch);
}
curl_close($ch);
$result is always false so the error triggers with the above message shown.
I tried
I found this https://stackoverflow.com/a/59955444/1092632 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
causes a timeout of the endpoint
Using curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); instead of curl_setopt($ch, CURLOPT_PUT, true);
no change
Adding $headers[] = 'Connection: close';
no change
Where am I wrong sending the PUT request? Any hint highly appreciated!
Curl code I am using in php but it is not working
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/unsubscribes \
-F address='bob#example.com' \
-F tag='tag1'
try this
$ch = curl_init(); // initiate curl
$url = 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/unsubscribes';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "&address=" . $address . "&tag=" . $tag ); // define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
$output = curl_exec($ch); // execute
curl_close($ch);
CRUL to PHP code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/unsubscribes");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "api" . ":" . "YOUR_API_KEY");
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
I am trying to correctly convert a cURL command to PHP, but I can't seem to find the correct commands. I can run this command with cURL but I need to run this through TROPO, and they only have a few script options.
So I am trying in PHP.
curl https://api.particle.io/v1/devices/310029000547353138383138/setNum \ -d access_token=e650d0dec0476de7d64b23110fed31dcb3cbxxxx \ -d args=2085555555
My try, what am I missing?
<?php
function submitValue() {
$ch = curl_init("https://api.particle.io/v1/devices/310029000547353138383138/setNum");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'access_token=e650d0dec0476de7d64b23110fed31dcb3cbxxxx&"args=2085555555"');
);
$output = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != '200') {
return null;
}
return $output;
}
?>
Try to use cURL command below:
curl --data "access_token=e650d0dec0476de7d64b23110fed31dcb3cbxxxx&args=2085555555" https://api.particle.io/v1/devices/310029000547353138383138/setNum
Got this answer from https://superuser.com/a/149335
UPDATED:
Here I have provided PHP code:
$data = json_decode(array(
"access_token" => "e650d0dec0476de7d64b23110fed31dcb3cbxxxx",
"args" => "2085555555"
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.particle.io/v1/devices/310029000547353138383138/setNum');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
Hope this will help
I need to "translate" this curl in php, but I can't do it:
curl --proxy proxy_ip:port --proxy-user user:psw- --user "admin":"admin" \
-H 'Content-type: application/json' -X PUT \
-d '{"installInHw":"true", "name":"flow1", "node": {"id":"00:00:00:4e:54:32:33:0f", "type":"OF"}, "ingressPort":"2", "etherType": "0x800", "protocol": "6", "tpDst": "80", "priority":"65535","actions":["DROP"]}' \
'indirizzo_http'
Can anybody helps me?
PHP curl has these options to proxy requests.
// $data = '{"installInHw":"true", "name":"flow1", "nod" ... ;
//$loginpassw = 'admin:admin';
//$proxy_ip = '192.168.0.50';
//$proxy_port = '8080';
//$url = 'http://example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
if(!$response) {
return false;
}
Thanks everybody!
I solved the problem adding this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
I'm attempting "the first call" as outlined by the Paypal API documentation. This is the example provided that I'm following:
curl https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
-d "grant_type=client_credentials"
I have constructed a curl instance in PHP with all the above headers apart from the last one. What does a -d flag convert to as a curl option in PHP? There is little explanation there as far as I can tell. I managed to deduce -u as CURLOPT_USERPWD.
Having a good trawl around I pieced together parts from developers with other problems. I successfully gained my access token using the following code:
<?php
$ch = curl_init();
$clientId = "myId";
$secret = "mySecret";
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);
print_r($json->access_token);
}
curl_close($ch);
?>
#Lee point the right way but if u are using and old php version it wont work. But Lee version will not show up the error. Use these instead, i only add the error part to see what is going on.
$ch = curl_init();
$clientId = "";
$secret = "";
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);
$err = curl_error($ch);
$access_token="";
if ($err) {
echo "cURL Error #:" . $err;
}
else
{
$json = json_decode($result);
// print_r($json->access_token);
$access_token = $json->access_token;
}
If u are having and old PHP version it is posible it wont work and it will show this error:
cURL Error #:error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure
Just stack overflow it!
Here they discuss the problem
JSON and US English appear to be the Defaults, but to be in perfect compliance, add the following line:
curl_setopt($ch, CURLOPT_HTTPHEADER, "Accept: application/json, Accept-Language: en_US");