PHP curl_setopt equivalent to curl -d - php

I got the following curl command to work on Linux:
curl -H "Content-Type:application/json" -H "Accept:application/json" -H "Authorization: Basic dGVsZXVuZzpuYWcweWEyMw==" -X PUT -d '{"requireJiraIssue": true, "requireMatchingAuthorEmail": "true"}' http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc%3AyaccHook/enabled
However, when I tried to do this on PHP, the data is not being sent to the server properly, this is my set_opt commands:
$myURL = "http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled";
$hookdata_yacc = array(
'requireJiraIssue' => true,
'requireMatchingAuthorEmail' => true
);
$data = json_encode($hookdata_yacc);
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic dGVmyPasswordEyMw==",
"Content-Length: " . strlen($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
return (curl_exec($curl));
What did I miss?

You use CURL options improperly. CURLOPT_PUT is intended for sending files, it is not suited for your case. You have to use CURLOPT_CUSTOMREQUEST option and set it to "PUT", not "POST".
So the code should look like this:
$myURL = "http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled";
$hookdata_yacc = array(
'requireJiraIssue' => true,
'requireMatchingAuthorEmail' => true
);
$data = json_encode($hookdata_yacc);
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic dGVmyPasswordEyMw==",
"Content-Length: " . strlen($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
return (curl_exec($curl));

Related

Updating WooCommerce product via REST API using PHP CURL

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

CURL call is working fine directly in CURL but not in PHP 7.3

I want to make a simple call to an API from PHP. It's working fine directly in CURL but not in PHP 7.3.
My real token was replace by TOKEN
CURL:
curl -X POST -H 'Authorization: Bearer TOKEN' -H "Content-Type: application/json" --data-binary '{"query":"{cameraList{name}}"}' https://cloud.camstreamer.com/api/graphql.php
PHP:
<?php
$url = "https://cloud.camstreamer.com/api/graphql.php";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers["Authorization"] = "Bearer TOKEN";
$headers["Content-Type"] = "application/json";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{"query": "{cameraList{name}}"}';
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);
var_dump($resp);
exit;
You can use
$info = curl_getinfo($ch);
$error = curl_error($ch);
to see what happened.
The content type was not sent correctly.
I changed this:
$headers = array();
$headers["Authorization"] = 'Bearer TOKEN';
$headers["Content-Type"] = "application/json";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
To this:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer TOKEN'
));

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

Converting cli curl to php url

I have problem with converting cli cURL to php cURL version.
I've tested such link from cli:
curl -H "X-API-KEY: key" -XPOST -v -H "Accept: application/json" http://sentione.com/api/statements/search -H "Content-Type: application/json" -d '{"topicId":id, "from": "2014-01-01 00:00:00.000 CET"}'
and it's working properly.
And that's my code.
I got no response.
$headers = array(
"Content-type: application/json",
"X-API-KEY: key",
"Accept: application/json"
);
$arr = array(
"topicId" => id,
"from" => "2014-01-01 00:00:00.000 CET"
);
$url = "http://sentione.com/api/statements/search";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($arr));
$response = curl_exec($curl);
print_r($response);
Of course I set correct api key and topic id.
What am I doing wrong?
My bad, see at my array declaration.
I used : instead of =>. I don't know why.
It's solved, thanks anyway! :)

Execute SOAP using cURL

I am trying to execute an SOAP-function using cURL (because I get an error using the SoapClient().
This is my code (that is halfway working)
$credentials = "username:pass";
$url = "https://url/folder/sample.wsdl";
$page = "/folder";
$headers = array(
"POST ".$page." HTTP/1.0",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"customerSearch\"",
"Authorization: Basic " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
$data = curl_exec($ch);
The problem is that the SOAP-action is not being performed. And I also need to pass arguments to the action. Is this even possible?
You need to specify the cURL options to POST, and set the body of the request - if your not sending a body, there's no point in a POST request (and more importantly, it isn't SOAP). Building a complete HTTP request header just won't cut it.
$credentials = "username:pass";
$url = "https://url/folder/sample.wsdl";
$body = ''; /// Your SOAP XML needs to be in this variable
$headers = array(
'Content-Type: text/xml; charset="utf-8"',
'Content-Length: '.strlen($body),
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'SOAPAction: "customerSearch"'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
// Stuff I have added
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
$data = curl_exec($ch);

Categories