Using curl to post an array to the godaddy api - php

I am trying to post a bunch of domains to the godaddy api in order to get information about pricing and availability. However, whenever I try to use curl to execute this request, I am returned nothing. I double checked my key credentials and everything seems to be right on that end. I'm pretty confident the issue is in formatting the postfield, I just don't know how to do that... Thank you to whoever can help in advance!
$header = array(
'Authorization: sso-key ...'
);
$wordsArray = ['hello.com', "cheese.com", "bytheway.com"];
$url = "https://api.godaddy.com/v1/domains/available?checkType=FAST";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_POST, true); //Can be post, put, delete, etc.
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $wordsArray);
$result = curl_exec($ch);
$dn = json_decode($result, true);
print_r($dn);

There are two problems in your code:
Media type of sent data must be application/json (by default this is application/x-www-form-urlencoded), and your PHP app must accept application/json as well:
$headers = array(
"Authorization: sso-key --your-api-key--",
"Content-Type: application/json",
"Accept: application/json"
);
Post fields must be specified as JSON. To achieve this, use the json_encode function:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($wordsArray));
Full PHP code is:
$headers = array(
"Authorization: sso-key --your-api-key--",
"Content-Type: application/json", // POST as JSON
"Accept: application/json" // Accept response as JSON
);
$wordsArray = ["hello.com", "cheese.com", "bytheway.com"];
$url = "https://api.godaddy.com/v1/domains/available?checkType=FAST";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($wordsArray));
$result = curl_exec($ch);
$dn = json_decode($result, true);
print_r($dn);

Related

How to Retrieve cURL Data within the URL Endpoint

I am trying to create a cURL api, let's say the below is my curl request code, where http://localhost/api/endpoint.php is my endpoint. What would actually be going on, code-wise in the endpoint.php file? How do I actually get the data that is sent via the cURL request, like the CURLOPT_USERPWD data, in the endpoint.php page? Thanks.
$ch = curl_init();
$url = 'http://localhost/api/endpoint.php';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERPWD, 'alex:password123');
$headers = array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
'Accept-Language: en_US'
);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$vars['grant_type'] = 'client_credentials';
$req = http_build_query($vars);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
$response = curl_exec($ch);
You can access to these data via the $_SERVER superglobal.
Endpoint :
<?php
$user = $_SERVER['PHP_AUTH_USER']; // alex
$password = $_SERVER['PHP_AUTH_PW']; // password123
in your file endpoint.php you can print your posted value using
print_r($_POST);
after your curl print response like
print_r($response);

PHP cURL how to send a JSON POST request and also include a URL querystring?

I'm trying to submit a POST request with JSON data to an api endpoint. The endpoint requires a querystring passing the api credentials, but also requires the JSON data to be POSTed.
When I try to do this with PHP cURL as shown below, the querystring is apparently removed - thus the api is rejecting the request due to missing api key.
I can do this easily with Postman when testing access to the api endpoint.
How can I make the cURL request include both the querystring AND the JSON POST body?
Example code:
// $data is previously defined as an array of parameters and values.
$url = "https://api.endpoint.url?api_key=1234567890";
$ch = curl_init();
$json = json_encode($data);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
]
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
You are doing almost right.
Sometimes you need to relax SSL verification.
Otherwise, update php ca bundle:
https://docs.bolt.cm/3.7/howto/curl-ca-certificates
Add the following:
$headers = array(
"Content-type: application/json;charset=UTF-8",
"Accept-Encoding: gzip,deflate",
"Content-length: ".strlen($json),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, "identity, deflate, gzip");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Sometimes you need to change encoding too:
$result = utf8_decode($result);
And check the returning data.

How to send Arabic SMS using Clickatell REST API in PHP?

Here is my code
$to="[\"<mobile number>\"]";
$text = "غثس هفس خن";
$arr = unpack('H*hex', iconv('UTF-8', 'UCS-2BE', $text));
$message = strtoupper($arr['hex']);
$authToken="3fUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickatell.com/rest/message");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"text\":\"$message\",\"to\":$to}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Version: 1",
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Bearer $authToken"
));
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
When I am sending using this, I am getting some numbers as SMS
From Clickatell document, I got one solution that add a parameter named 'unicode' as '1'.
Then I changed the parameter line like this
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"text\":\"$message\",\"to\":$to\",\"unicode\":1}");
Now I am getting {"error":{"code":"100","description":"Data malformed","documentation":"http://www.clickatell.com/help/apidocs/error/100.htm"}}

Curl xml Response in Php

i need to post Data in Xml to a server but whnen i use a basic curl post i have a redirect page to the curl url , but when i use something like postman it works just find , I even tried the code generated by postman and i got the same results (redirect)
here is my curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "URL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ["SAMLResponse"=> $xml]);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "content-type: application/x-www-form-urlencoded";
$headers[] = "authorization: Basic CODE";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = (curl_exec($ch));

How to write PHP curl DELETE request

I'm referencing DigitalOcean's API docs, and they give the following example on how to delete a droplet here:
curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer API_TOKEN" "https://api.digitalocean.com/v2/droplets/[DROPLET_ID]"
How can I write this in PHP curl?
I currently have this:
$ch = curl_init('https://api.digitalocean.com/v2/droplets/18160706');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer MY_API_TOKEN')
);
$result = curl_exec($ch);
echo $result;
But that is not deleting my droplet and is returning (Yes, the droplet id is correct):
{"id":"not_found","message":"The resource you were accessing could not be found."}1
Try This
Change the CUTLOPT_CUSTOMREQUEST to "DELETE" Find More
$ch = curl_init('https://api.digitalocean.com/v2/droplets/18160706');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer MY_API_TOKEN')
);
$result = curl_exec($ch);
echo $result;
You need to set CURLOPT_CUSTOMREQUEST to DELETE as mentioned in the PHP Manual here. Please take some time to read it. The following code should work
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode($result);
curl_close($ch);

Categories