Updating WooCommerce product via REST API using PHP CURL - php

It seems as if $data isn't correct. I can list products and I get the whole product JSON as a response. But the product in WooCommerce doesn't change the price. When I do the same thing via curl command line, the update is working.
Referring to that: https://woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product
What am I doing wrong?
<?php
$url = "https://wooexample.com/wp-json/wc/v3/products/455";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/json",
"Authorization: Basic " . base64_encode ( 'ck_33fbff7b90dcddba9bd4cbedaeda6b2fa3:cs_4156da18fc357b288e42a7e7b75fa6682b' ),
);
// print_r($headers);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = [
'regular_price' => '24',
'price' => '24'
];
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
print_r(json_decode($resp));

Ty this one, I have checked, it's working perfectly for me.
<?php
$url = "https://localhost/wordpress/wp-json/wc/v3/products/455";
$consumer_key = 'your consumer key put here..';
$consumer_secret = 'your consumer secre put here..';
$headers = array(
'Authorization' => 'Basic ' . base64_encode($consumer_key.':'.$consumer_secret )
);
$data = array(
'regular_price' => '24',
'price' => '24'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, "$consumer_key:$consumer_secret");
$resp = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
print_r(json_decode($resp));

The problem lies on the data format you are sending.
the docs is using a library that's supposedly transform the data already in a supported format, while your curl request is posting raw array data via curl
you can try transforming your data into a form data with http_build_query
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data) );
or convert them into JSON format
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data) );
EDIT
You also appear to have wrong curl request, you're passing the keys on as base64_encoded authorization in headers, but based on the docs you're supposed to do
curl -X PUT https://example.com/wp-json/wc/v3/products/794 \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"regular_price": "24.54"
}'
Your php curl request should have be something like
$headers = [
"Content-Type: application/json",
]
.
.
curl_setopt($curl, CURLOPT_USERPWD, "your_key_here" . ":" . "your_secret_here");

Related

Post request empty response PHP curl

When trying to send a post request to the server, the response returns an empty array "{}". But there is an answer on postsman. Presumably this is related to the content type of the response
<?php
$url = "https://api.business.kazanexpress.ru/api/oauth/token?
grant_type=password&username={}&password={}";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Authorization: Basic token",
"Content-Type: application/json",
"Content-Length: 0",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
Usually the OAuth2 provider will want you to send the data inside the POST body and not in the URL. To do this you can send an array with your grant_type, username and password using CURLOPT_POSTFIELDS and remove the Content-Type and Content-Length headers since cURL will generate them for you when using CURLOPT_POSTFIELDS.
It should look something like this
<?php
$url = "https://api.business.kazanexpress.ru/api/oauth/token";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, [
'username' => '',
'password' => '',
'grant_type' => 'password'
]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = [
"Authorization: Basic token",
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

php curl GET request with cookies

I am using the below code to initiate a GET request but I get only "status":403 from the JSON. The JSON needs a cookie to access. So I added the cookie. What mistake am I making here? Why do I get 403 status and can't access JSON?
$url = "URL HERE";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Cookie: ci_session=COOKIE HERE",
"Accept: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

Is there anything wrong with my request when CURL POST request returns error 55

I'm trying to make a POST request using curl in php and am getting curl error #55. Have been looking around and don't see much information on this error. I don't think my request is formed wrong.
$curl = curl_init();
$encoded = base64_encode("12345:12345");
curl_setopt($curl, CURLOPT_URL, "https://accounts.spotify.com/api/token");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json\r\n",
"Accept: application/json",
'Authorization: Basic ' . $encoded,
"Accept: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
"grant_type" => "refresh_token",
"refresh_token" => 'CxRmNSepUaA41Zs8xIN68fC2wYbRrsrQWKE9547fF6Q12LDOpr551xb95K62+EqnGq6glEHXF4R3qgtgCnOVpr9wFAaxta5/iBKzYBqk+2B442qgiUQu7GyAm+mD1ick3vGrdlZgEEpr/U9EcVVGqXf3XN1EhlZa/vYGgO2opkKedFgbN53Hdd+xQ=='
));
$server_output = curl_exec($curl);
$err = curl_errno($curl);
print("CURL error: ");
print($err);
print($server_output);

Bluemix PHP Watson Personality Insights Service

What is the error in the code below? It returns this error:
{ "error":"Internal server error (root cause: multipart\/form-data; boundary=----------------------------248f475465f9)",
"code":404 }
Code:
<?php
function testLangID($data) {
$curl = curl_init();
$headers_arr = array(
"contentItems" => array(
"userid" => "dummyuserid",
"id" => "dummyid",
"sourceid" => "freetext",
"contenttype" => "application/json",
"language" => "en",
"content" => $data
)
);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $headers_arr);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "........:........");
curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
$decoded = json_decode($result, true);
return $result;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$res= testLangID( $_POST["textLID"] );
echo $res;
}
?>
The url you are using:
curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api");
should be:
curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api/v2/profile");
I saw you are using contentItems, you can also sent content-type: text/plain and the text in the body no need to build the JSON object.
The curl command to send text in the body is:
curl -X POST -u USERNAME:PASSWORD \
-H "Content-Type: text/plain" \
-d "Text to analyze" \
"https://gateway.watsonplatform.net/personality-insights/api/v2/profile"
I answered this in dwAnswers earlier this morning.
You only need the "body" parameter in the POST request (and "headers" if not using default value). As described in the API documentation the other parameters are part of the HTTP HEADER request.
Here is the PHP code modified to work with Watson Personality Insights:
$curl = curl_init();
$post_args = array(
'body' => $data
);
$header_args = array(
'Content-Type: text/plain',
'Accept: application/json'
);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_args);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD,<user id>:<password>");
curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api/v2/profile");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
$decoded = json_decode($result, true);

Twilio cURL calling a phone

Based on the documentation of Twilio and Curl I have a php curl routine:
function twilio($mobile,$msg,$twoCode){
$url = 'https://api.twilio.com/2010-04-01/Accounts/'.TWILIO_ACCOUNT_SID.'/Calls.json';
$CallURL = 'http://Myweb.com/code/say/'.$twoCode;
$auth = TWILIO_ACCOUNT_SID.":".TWILIO_AUTH_TOKEN;
$fields = array(
'To' => $mobile ,
'From' => '+16262471234' , // My Number
'Url' => urlencode( $CallURL ) ,
'Method'=>'GET' ,
'FallbackMethod'=>'GET',
'StatusCallbackMethod'=>'GET',
'Record'=>'false'
);
$post = http_build_query($fields);
$curl = curl_init($url);
// Set some options - we are passing in a useragent too here
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT , 'Mozilla 5.0');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 7' ));
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_VERBOSE , true);
$resp = curl_exec($curl);
curl_close($curl);
}
It gives me an error:
{"code": 21213, "message": "No 'From' number is specified", "more_info": "https://www.twilio.com/docs/errors/21213", "status": 400}
I tried all the options, can anyone help?
I edited and added a "+" for the "From" number. But still the error remains the same.
Thanks in advance!
Twilio Developer Evangelist here.
Your code is really close. Just two small changes should resolve your issue. First you need to remove this line:
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 7' ));
This was truncating your POST data which resulted in the "From" information not being sent.
Second, you don't need to urlencode the $CallURL because curl handles this for us.
Once you make both of these changes your code should look like this and run without an error:
function twilio($mobile,$msg,$twoCode){
$url = 'https://api.twilio.com/2010-04-01/Accounts/'. TWILIO_ACCOUNT_SID.'/Calls.json';
$CallURL = 'http://Myweb.com/code/say/'.$twoCode;
$auth = TWILIO_ACCOUNT_SID .":". TWILIO_AUTH_TOKEN;
$fields = array(
'To' => $mobile ,
'From' => '+16262471234' , // My Number
'Url' => $CallURL,
'Method'=>'GET' ,
'FallbackMethod'=>'GET',
'StatusCallbackMethod'=>'GET',
'Record'=>'false'
);
$post = http_build_query($fields);
$curl = curl_init($url);
// Set some options - we are passing in a useragent too here
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT , 'Mozilla 5.0');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_VERBOSE , true);
$resp = curl_exec($curl);
curl_close($curl);
}
Let me know if this gets your issues resolved.

Categories