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",
[...]
);
Related
I need to update/create shopify order fulfillment but getting some errors like
{
"errors": "Not Found"
}
I have checked creds everything is right key token store URL everything but getting this error again and again. is there someone who can help with this?
Code is copied and implemented from this guide Fulfillments orders
header("Access-Control-Allow-Origin: *");
set_time_limit(0);
error_reporting(E_ALL);
ini_set("display_errors", 1);
define("API_KEY", '8f6ba81c2bXXXXXXXXXXXXXXXXX');
define("ACCESS_TOKEN", 'XXXXXXXee88e7dXX3604d8XXXXXXXXXXXXXX');
define("SHOP", 'myshopistore-XXXXX');
define("API_VERSION", '2022-10');
$ch = curl_init();
echo $url = 'https://'.API_KEY.':'.ACCESS_TOKEN.'#'.SHOP.'.myshopify.com/admin/api/2022-10/fulfillments.json';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"{"fulfillment":{"message":"The package was shipped this morning.",
"notify_customer":false,"tracking_info":{"number":1562678,"url":"https://www.my-shipping-company.com","company":"my-shipping-company"},
"line_items_by_fulfillment_order":[{"fulfillment_order_id":125846255999999}]}}");
$headers = array();
$headers[] = 'X-Shopify-Access-Token: XXXXXX27eeXX8e7d9e3604dXXXXXXXXXXXXXX';
$headers[] = 'Content-Type: application/json';
print_r($headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo $result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Here is the answer of the question this can help someone in the same case, actually I was using wrong fulfillment_order_id and location_number
Before creating a fulfillment from above code we need to get the fulfillment_order_id from this API request https://'.API_KEY.':'.ACCESS_TOKEN.'#'.SHOP.'.myshopify.com'/admin/api/2022-10/orders/XXXXXXXXXXXXXX/fulfillment_orders.json
this request will return the fulfillment_order_id inside the line_items node then use this id to create your fulfillment.
below API will return locations in case you need them
https://'.API_KEY.':'.ACCESS_TOKEN.'#'.SHOP.'.myshopify.com'/admin/api/2022-10/locations.json
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));
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.
I am using rest api to fetch basic profile data from linkedin with php code. I am successfully able to generate access code and access token but I am getting ssl required error whenever I tried to get basic profile using following url
https://api.linkedin.com/v1/people/~?format=json
I followed all steps to make authenticated requests as listed there https://developer.linkedin.com/docs/oauth2
I am using a non ssl url as call back parameter , Is it necessary to use ssl url ? If not then why I am getting this error .
looking for a solution
below is code to get profile fields
$host = "api.linkedin.com";
$path = "/v1/people/~)";
//$arr = array('oauth2_access_token' => $access['access_token']);
$token = $access['access_token'];
$header[] = "Authorization: Bearer ".$token;
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$ch = curl_init();
// endpoint url
curl_setopt($ch, CURLOPT_URL, $host . $path);
// set request as regular post
//curl_setopt($ch, CURLOPT_HTTPGET, true);
// set http version
curl_setopt($ch, CURLOPT_HTTP_VERSION, 'HTTP/1.1');
// set data to be send
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr));
// set header
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// set header
curl_setopt($ch, CURLOPT_CERTINFO, true);
// return transfer as string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_export($response);
My solution was to use https:// instead of just http://
Trying to use folder grid api to upload a file. There are 2 steps.
Calling provision a file to get a location with a guid.
Uploading the actual file using a PUT.
I am able to do the first call and get a location of the type https://files.foldergrid.com/upload/xxx. I then try to upload the file using this as my location and keep getting
< HTTP/1.1 100 Continue
* Empty reply from server
* Connection #0 to host files.foldergrid.com left intact
* Closing connection #0
from the server not sure what I am missing?
Here is the code I am using:
After calling the provision api I get back a url which I store in
$url = 'https://files.foldergrid.com/upload/xxx'
and I have the path to the file I want to upload in file
$file = 'some path to file'
then I have call the following method:
public function putFile($file,$url) {
$headers = array(
'fg-eap: false',
'fg-md5:'.md5_file($file),
);
$fh = fopen($file, "rb");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl,CURLOPT_PUT,true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_INFILE, $fh);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file));
curl_setopt( $curl, CURLOPT_VERBOSE, true );
$result = curl_exec($curl);
fclose($fg);
curl_close($curl);
return $result;
}
The code you've posted looks to have everything needed except the Content-Type header. Try setting your headers array as:
$headers = array(
'fg-eap: false',
'fg-md5:'.md5_file($file),
'Content-Type: binary/octet-stream'
);
Also, in response to your query here we've posted an updated PHP client sample under our API documentation here.