How to create or update shopify fulfillments - shopify

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

Related

convert a curl request to php-curl - different responses received

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));

getList() must be an instance of Magento\Framework\Api\SearchCriteriaInterface

Been trying to figure out what is going on here, but gotten stuck. Anyone got an idea as to what is going on.
Using Magento 2.2.4
( ! ) Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Catalog\Model\ProductRepository\Interceptor::getList() must be an instance of Magento\Framework\Api\SearchCriteriaInterface, string given in C:\wamp64\www\magento2\generated\code\Magento\Catalog\Model\ProductRepository\Interceptor.php on line 85
( ! ) TypeError: Argument 1 passed to Magento\Catalog\Model\ProductRepository\Interceptor::getList() must be an instance of Magento\Framework\Api\SearchCriteriaInterface, string given in C:\wamp64\www\magento2\generated\code\Magento\Catalog\Model\ProductRepository\Interceptor.php on line 85
Code I try to execute:
set_time_limit(0);
define('TOKEN', 'token code');
define('URL', 'http://localhost:8080/magento2/index.php');
$headers = array("Authorization: Bearer ". TOKEN);
//API URL to get all Magento 2 modules
$requestUrl = (URL . "/rest/V1/products?searchCriteria=");
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
You need to pass search Criteria while requesting API. Try below code.
set_time_limit(0);
define('TOKEN', 'token code');
define('URL', 'http://localhost:8080/magento2/index.php');
$headers = array("Authorization: Bearer ". TOKEN);
//API URL to get all Magento 2 modules
$requestUrl = (URL . "/rest/V1/products/?searchCriteria[filter_groups][0][filters][0][field]=category_gear&searchCriteria[filter_groups][0][filters][0][value]=86&searchCriteria[filter_groups][0][filters][0][condition_type]=finset");
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);

ZERO_RESULTS error geocode

$url = "https://maps.google.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo "<pre>"; print_r($response_a);
i am using this code to receive data but all i get is ZERO_RESULTS. but the thing is, the url works when i use it in the browser, it just doesnt work with curl. any ideas?
here is a url that works in browser but does not work with curl:
https://maps.google.com/maps/api/geocode/json?address=Wiener+Stra%C3%83%C5%B8e+86+3830+Waidhofen+an+der+Thaya&sensor=false
It looks like something is wrong with encoding of 'Straße'.
In your sample code you have 'Stra%C3%83%C5%B8e'. Let's use this utility http://meyerweb.com/eric/tools/dencoder/ end decode this value.
You will get 'Straße'. Now let's encode 'Straße' with this utility and we will get 'Stra%C3%9Fe'.
So at this point it looks like you should have this query issued by your PHP code
https://maps.google.com/maps/api/geocode/json?address=Wiener+Stra%C3%9Fe+86+3830+Waidhofen+an+der+Thaya&key=YOUR_API_KEY
I removed sensor parameter as it's not required anymore.

Get products of an order in Bigcommerce

I need a little help regarding to BigCommerce API. I wanted to ask that how can I write this statement
GET orders/{id}/products.json
in php.
Basically I am trying to get products from a specific order. IF I type it in command prompt it returns me the products in the shel but i wanna run it in php.
Any ideas?
Let me know thanks.
You can make a cURL request to BigCommerce Products API
Following is a simple cURL snippet to get products details:
<?php
$api_url = 'https://XYZ.mybigcommerce.com/api/v2/orders/201/products.json';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, array ('Accept: application/json', 'Content-Length: 0') );
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_USERPWD, "admin:YOUR API KEY" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$response = curl_exec( $ch );
$result = json_decode($response);
echo "<pre>";
print_r($result);
echo "</pre>";
?>

Uploading files to RapidShare using PHP and cURL via their API

I'm trying to upload files to RapidShare using their API and PHP with cURL. So far, I've got this code:
// Get the RapidShare server to upload to
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$uploadServer = curl_exec($ch);
// Upload the file to RapidShare
$url = 'http://rs' . $uploadServer . '.rapidshare.com/cgi-bin/rsapi.cgi?sub=upload';
$url .= '&login=login';
$url .= '&password=mypass';
$url .= '&filename=' . $filename;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
// Set post fields
$postFields = array('filecontent' => array('#' . $targetDir . '/' . $id));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$resp = curl_exec($ch);
die($resp);
But the only response I get is: ERROR: Subroutine invalid. (b6ba5d82). If I just do this (basically don't send the file with the request):
// Upload the file to RapidShare
$url = 'http://rs' . $uploadServer . '.rapidshare.com/cgi-bin/rsapi.cgi?sub=upload';
$url .= '&login=login';
$url .= '&password=mypass';
$url .= '&filename=' . $filename;
curl_setopt($ch, CURLOPT_URL, $url);
$resp = curl_exec($ch);
I get this response: ERROR: No files transmitted. (f269d341)
So I'm guessing there's something wrong with the way I'm sending the file via POST.
Anyone know what could be wrong?
Thank you.
Oddly enough, i just hit this problem as well. after trying for an hour or two to find out what's wrong, it turned out to be quite simple and caused by ignoring a single statement in the api document : "GET parameters are ignored if the POST method is used. So if you use the POST method, send ALL parameters via POST." for upload to work, sub=upload should also be sent as a POST parameter rather than GET.
Add curl parameter
curl_setopt($ch, CURLOPT_INFILESIZE,(string)filesize($filename));
curl_setopt($ch, CURLOPT_INFILE,fopen($filename,'r'));
and $filename must write full path.
Sorry Im english very little.