Iam using MX API and want to convert curl command to php. The command is as follows:
curl -i -X GET 'https://vestibule.mx.com/institutions' \
-H 'Accept: application/vnd.mx.atrium.v1+json' \
-H 'MX-API-Key: de9f1ed2-6332-ff80-0493-11ea9952cfb3' \
-H 'MX-Client-ID: c3d38602-3a95-401f-9116-8fcedfa87dfd'
Any help would be highly appreciated
Google is your friend:
There is a PHP-plugin which offers an API to use curl: https://secure.php.net/manual/de/book.curl.php
And you can use a class by pyromus and extend it to your needs: https://secure.php.net/manual/de/book.curl.php#86391
Paste your curl command into curlconverter.com/php/ and it will convert it into this PHP code using PHP's cURL integration:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://vestibule.mx.com/institutions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept' => 'application/vnd.mx.atrium.v1+json',
'MX-API-Key' => 'de9f1ed2-6332-ff80-0493-11ea9952cfb3',
'MX-Client-ID' => 'c3d38602-3a95-401f-9116-8fcedfa87dfd',
]);
$response = curl_exec($ch);
curl_close($ch);
Related
I have this request to update a user using the user provisioning API in owncloud.
curl -X PUT https://example.com/ocs/v1.php/cloud/users/pinuccio -d 'key=email' -d 'value=jack#google.com' -H "OCS-APIRequest: true" -u 'admin:adminpwd'
I am trying to convert it to PHP CURL.
I ended up so far with this:
$username = 'admin';
$password = 'adminpwd';
$postData = array(
'key' => 'email',
'value' => 'jack#google.com'
);
$ch = curl_init('https://thesmartred.com/cloud/ocs/v1.php/cloud/users/'.$userid);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HEADER, "OCS-APIRequest: true");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
but I get a 997 response. While if I run this on shell I get a 405. Why do I get two different answers?
Anyone can help?
Here are couple of differences between your commandline request and php curl request.
First, You are using CURLOPT_HEADER in a wrong way. In php curl manual it says
CURLOPT_HEADER TRUE to include the header in the output
So to add a header in a proper way, example is:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'OCS-APIRequest: true'
));
Second, you are passing the array to your post fields. For array it does a multipart/form-data post. You need to pass data as string, and http_build_query() comes handy this case.
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
I'm trying to use abuseipdb API2 to check IP address reputations fro my email servers
The example given in the docs only covers shell curl
the problem is that I cannot convert shell curls --data-urlencode "ipAddress=118.25.6.39" into php-cURL
GEt variable does not work, post is not permitted, headers containing the IP address are not accepted>
All fails with : "detail":"The ip address field is required.","status":422
My Code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.abuseipdb.com/api/v2/check?'.urlencode("ipAddress=127.0.0.2") );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = 'Key: <API-KEY>';
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
abuseipdb API2 Code:
curl -G https://api.abuseipdb.com/api/v2/check \
--data-urlencode "ipAddress=118.25.6.39" \
-d maxAgeInDays=90 \
-d verbose \
-H "Key: $YOUR_API_KEY" \
-H "Accept: application/json"
this works for me.
//Setup
$api_key = 'REPLACE_YOUR_KEY';
$abuseipdb_endponint = 'https://api.abuseipdb.com/api/v2/check';
$ipToCheck = 'REPLACE_IP';
$headers = array(
'Key: ' . $api_key,
'Accept: application/json'
);
//cURL Work
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curlObj, CURLOPT_URL, ($abuseipdb_endponint . '?ipAddress=' . $ipToCheck));
curl_setopt($curlObj, CURLOPT_HTTPGET, 1);
$curl_response = curl_exec($curlObj);
print_r($curl_response);
curl_close($curlObj);
Response example:
{"data":{"ipAddress":"79.127.127.253","isPublic":true,"ipVersion":4,"isWhitelisted":false,"abuseConfidenceScore":100,"countryCode":"IR","usageType":"Data Center\/Web Hosting\/Transit","isp":"AsiaTech Data Transfer Inc PLC","domain":"asiatech.ir","totalReports":80,"lastReportedAt":"2019-04-24T01:44:51+01:00"}}[Finished in 0.9s]
You can use this to get Verbose data, all in an HTTP GET Query.
$key = "key";
$ip = "1.1.1.1";
$data = file_get_contents("https://api.abuseipdb.com/api/v2/check?ipAddress=$ip&maxAgeInDays=90&verbose&key=$key");
var_dump($data);
Using Postman, I have been able to successfully create an API call to retrieve information from the webserver.
However, I have been unsuccessful in doing the same with PHP. Following are a few screenshots from Postman which I am trying to replicate in PHP.
This is my PHP code -
$filters = array("1234", "28");
$selectors = array("Brand", "Name");
$body = array('Filter' => array('SKU'=>$filters, 'OutputSelector' => $selectors));
$url = "https://xyz.neto.com.au/do/WS/NetoAPI";
$method = "POST";
$headers = array(
"NETOAPI_ACTION: GetItem",
"NETOAPI_KEY: xxxxxxx",
"Accept: application/json",
"NETOAPI_USERNAME: puneeth"
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
P.S I have deliberately fudged the url and key.
I am trying to follow instructions as outlined here - https://developers.neto.com.au/documentation/engineers/api-documentation/products/getitem
Few things that might be preventing your code from working:
1. Array to string conversion
You have declared the body of the request as an array in $body. You need to JSON-encode it before passing it to cURL:
$body = array('Filter' => array('SKU'=>$filters, 'OutputSelector' => $selectors));
$bodyJSON = json_encode($body);
[...]
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyJSON);
2. HTTPS request
If you're getting an empty response, you'll also need to configure cURL for SSL communication. A quick fix for this is to ignore the remote server certificate validity:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Although this works, see this answer for a better solution.
3. Missing Content-Type
When sending raw postfields, some servers need to know what type of data you're sending. This is done by setting a Content-Type header:
$headers = array(
"Content-Type: application/json",
[...]
);
I am having an issue where my SSL certificate subject name 'does match' the target host name but yet it throws the error
bash-4.1$ curl -X GET --cacert ./server-cert.pem --cert ./client-cert.pem --key ./client-key.pem 'https://PHXC02NX7CBG3QD:9001'
curl: (51) SSL: certificate subject name 'PHXC02NX7CBG3QD' does not match target host name 'PHXC02NX7CBG3QD'
As it can be seen that both the names are matching yet an error is thrown for reasons unkonwn.
Any help will be appreciated .Thanks in advance!
Just pass CURLOPT_SSL_VERIFYHOST equals to FALSE in curl request
$url = 'https://aa.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if (curl_exec($ch) === false) {
echo 'Curl error: ' . curl_error($ch);
} else {
echo 'Operation completed without any errors';
}
$content = curl_exec($ch);
curl_close($ch);
Im trying to get the error message of the office 365 api with CURL but the body response is always null and the curl_error comes null too.
When I enable the header(commented in the code), the response comes with the header only with no body.
Here's an example code for the photo api where I write the wrong token to invoke the error
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://outlook.office.com/api/v2.0/me/photo");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$headers = array(
"Authorization: Bearer jdbvdbvdjkdvjdsvsd",
"Content-Type: application/json"
);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
// curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
echo 'Response : <br/>'.$response;
echo 'HTTP Code : '.curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo '<br/>Curl error:<br/> ' . curl_error($ch);
curl_close($ch);
curl_error($ch) returns the last error that occurred during a curl session. And this error is not HTTP error, rather it is the error that curl faced.
If you just comment out the two SSL options from your example code, then you'll see below error message from curl_error($ch) that your curl faced.
Curl error:<br/> SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
If you really need that status line message then you have to parse it from the response header. You can use regex or explode function of php.