Updating order WooCommerce API PHP CURL - php

I'm developing a web application for my co-workers, so they'll have an iPad with an application, to package our orders.
Whole app is almost finished, one of the last steps is to change order status from processing to completed.
What I'm doing:
Get current order ID, then change status with curl or API. What happens is very strange, I get JSON data back, and the completed_at time updated, but status is still processing.
Below you will see my code:
$data = array("status" => "completed");
$data_string = json_encode($data);
$username = 'XXX'; // Add your own Consumer Key here
$password = 'XXX'; // Add your own Consumer Secret here
$ch = curl_init('https://www.deallerleukste.nl/wc-api/v2/orders/5764?consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
echo $result;
Anyone who sees what I'm doing wrong? Please help!
Regards,
Jelmer

Try Using this code
$data='{ "order": { "status": "completed" }}';
or
Dynamically
$data=json_encode( array( 'order' => array( 'status' => $status)));
in $status you can pass the status u want.

I see it is a quite old post, but today I ran into the same problem, so maybe it is useful.
You have to use PUT instead of POST here:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
and data should be a valid JSON in this format:
$data='{"status": "completed"}';

Related

Not getting individual data using curl

i am trying to get indivdual data using curl from this website http://dummy.restapiexample.com/ .. I use the following code to get all data
<?php
$url='http://dummy.restapiexample.com/api/v1/employees';
$my_curl = curl_init();
curl_setopt($my_curl, CURLOPT_URL, $url);
curl_setopt($my_curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($my_curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($my_curl, CURLOPT_SSL_VERIFYPEER, 0);
$my_data = curl_exec($my_curl);
$my_json = json_decode($my_data);
echo '<pre>'; print_r($my_json ); echo '</pre>';
?>
1) This is working . But i want to get the indvidual data , so i tried to chage url and use the same code
$url="http://dummy.restapiexample.com/api/v1/employee/1"
But it is not working .
2) Also i try to create a new record , that is working
$data = array("name" => "Hagridbo", "salary" => "123", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://dummy.restapiexample.com/api/v1/create');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
But how can i get the response like data is inserted ? what data is inserted ?
1. Regarding individual employee data is not coming
If you look to the http://dummy.restapiexample.com/api/v1/employees, you will see that employee id 1 is not there. That's why you are not getting any data.
for example check data of this link:- http://dummy.restapiexample.com/api/v1/employee/20276
Also it seems that v1/employees API data changing on a regular interval.
2. Regarding created an employee successfully, but not getting any response.
do var_dump($result); after $result = curl_exec($ch);and see what it give you. I think it will give you a json data response of added user.
I said it based on details of that API link, check here: http://dummy.restapiexample.com/create

Why is PHP cURL PUT request not working despite getting status code 200 in return?

I've been stuck with this problem for a little while now. I am trying to use a REST API to change certain settings of a user like clear a user and set their device to inactive.
The REST calls are made in php which I am pretty new to. Most calls (get and post) are working just fine so I think I understood the basic concept of php and curl but I just can't get put requests working. The problem is that when making the REST call I get a status code 200 in return indicating that everything went fine but when I check the database nothing changed and the device is still active.
I've spent several hours researching this problem here on stackexchange (cURL PUT Request Not Working with PHP, Php Curl return 200 but not posting, PHP CURL PUT function not working) and additionally reading various tutorials.
To me my code looks fine and makes perfect sense because it is similar to many examples I found online. So please help me find my mistake.
$sn = "123456789";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/api/sn/".$sn);
$data = array("cmd" => "clearUser");
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$username = 'XXX';
$password = 'XXX';
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$output = curl_exec($ch);
curl_close($ch);
So far as I can see, you have two problems in your code.
Content-Type: application/json is incorrect, I would remove it entirely.
You don't have a Content-Length header.
I suggest trying
$sn = "123456789";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/api/sn/".$sn);
$data = array("cmd" => "clearUser");
$httpQuery = http_build_query($data);
$headers = array(
'Accept: application/json',
'Content-Length: ' . strlen($httpQuery)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$username = 'XXX';
$password = 'XXX';
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,$httpQuery);
$output = curl_exec($ch);
curl_close($ch);
You define in the Header 'Content-Type: application/json'. Try to encode your $data to json and then transfer the jsonEncodeteData:
$dataJson = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
perhaps this helps already.
status 200 may not be a success in the case of a PUT request. In a correct semantics (correct implementation of the server) successful PUT returns "201 Created" and, if the client sends empty or somehow wrong content, the server returns "204 No Content".
Lazy programmers may just return "200 Ok" instead of 204 with the meaning "your request is fine, but there is nothing to do with the data".
Try to verify your data and make sure that you send something that is not empty and is conformal to the API specs.

how to make a get call with JSON and retrieve data

I am trying to follow instructions from an RFC spec to retrieve data from "certificate transparency log" known logs here
Instructions say to make a JSON call but i don't seem to be having any success.. i actually don't get any data response at all.
Here is the guide I am trying to follow:
https://www.rfc-editor.org/rfc/rfc6962#section-4.6
Here is the php code I am using
<?php
$data = array("start" => "1", "end" => "10");
$data_string = json_encode($data);
$ch = curl_init('https://ct.googleapis.com/pilot/ct/v1/get-entries');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
var_dump(json_decode($result, true));
?>
The service I was connecting to has an SSL certificate installed and was not responding to my requests. Disabling the trust verification was the solution. I added this to the CURL options CURLOPT_SSL_VERIFYPEER => false

Shopify - Update/Delete metafield via metafield API using PHP

I've been stuck trying to update or delete a metafield that i created on user registration via the shopify customer API.
At first i tried to update the metafield using the customer API but it told me that the metafield already existed and i cannot add it again.
Then i used the user ID and made a call to retrieve all the metafields linked to the user using the metafield API - this worked and it returned all the metafields for me.
However my issue starts when i want to either delete or update the metafield that i've created.
I've tried a lot of different variations of REST calls but nothing seems to work.
To try and delete i used:
$baseUrl = "https://".$apikey.":".$password."#".$hostname."/admin/";
......
$curl = curl_init($baseUrl.'metafields/'.$metaID.'.json');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// Make the REST call, returning the result
$response = curl_exec($curl);
var_dump($response);
This did not do anything, so i tried going a level deeper and used the following URL:
$curl = curl_init($baseUrl.'customers/'.$custID.'/metafields/'.$metaID.'.json');
This URL did not work either.
Then I decided that i might as well just try and update the metafield to the new value that it was supposed to have.
I then tried the tried the following to update the value:
$metafield = array('metafields' => array(array(
'id' => $metaID,
'value' => '3',
'value_type' => 'string',
)));
$curl_url = $baseUrl.'customers/'.$custID.'/metafields/'.$metaID.'.json';
$ch = curl_init($curl_url);
$data_string = json_encode(array('metafield'=>$metafield));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);
After that i tried the alternative URL:
$curl_url = $baseUrl.'metafields/'.$metaID.'.json';
Any idea where i messed up?
Part 2 works now - creating and updating a metafield
So after writing and testing different sets of code i got something to work.
$data = array('metafield' =>
array(
'key' => 'level',
'value' => '1',
'value_type' => 'string',
'namespace' => 'Wholesaler'
)
);
$curl_url = $baseUrl.'customers/'.$custID.'/metafields.json';
//$ch = curl_init($baseUrl.$sid.'.json'); //set the url
$session = curl_init($curl_url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: POST') );
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if(ereg("^(https)",$curl_url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
var_dump($json);
echo "<br>-------------------------------------------------<br>";
echo $curl_url;
This code allowed me to create a metafield for a user that did not have it as well as update that metafield, as long as the key field and namespace makes it unique it will create a new field, if you use a key and namespace that already exists it will update the value.
I hope this helps someone else out there.
I do this call and it works perfect everytime:
DELETE /admin/customers/123456789/metafields/987654321.json
Where my customer ID is 123456789 and the metafield I want to kill has the ID 987654321
Part one is done - Delete
After doing alot of research and trying different pieces of script the following worked for me:
$curl_url = $baseUrl.'customers/'.$custID.'/metafields/'.$metaID.'.json';
//$curl_url = $baseUrl.'metafields/'.$metaID.'.json';
$session = curl_init($curl_url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if(ereg("^(https)",$curl_url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
var_dump($json);
I can now delete a metafield.

Push Wizard App not found error

I need to implement Pushwizard in PHP to send push notification.
I have implemented this php code
$api_key = "MY_API_KEY";
$msg = "Website PHP msg.";
$data = array("command" => "send", "message" => $msg);
$data_string = json_encode($data);
$ch = curl_init('https://pushwizard.com/api/'.$api_key.'/json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array ( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
$result = curl_exec($ch);
print $result;
But it gives me this error message.
{"error":"0002","error_msg":"app not found"}
I have check my configuration. From pushWizard admin panel I can able to send push notification, It works fine there. But from PHP api it is giving me an error.
I have googled it but not able to find any answer.
Does any own implemented it??
Thanks in advance.
You are probably trying to use the API with the App Key instead of the API Key.

Categories