Post request empty response PHP curl - php

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

Related

Curl PHP to API with HTTP Header and Post data conflicting

I am making a curl call using PHP to a PHP based URL and getting into some problems.
Please note that the when I add the Content-Type in the header, it causes the endpoint not to read my params at all.
Any help is appriciated.
Here is what I am doing and the result I am getting:
With Header but no Content-Type in Header:
$curl = curl_init();
$params="action=start_day_activity&woid=23";
$headers = [
"Authorization: Bearer $token"
];
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($curl);
// Status 200
// $result becomes = "{"flag":0,"message":"Token not found in request","data":[]}"
With Header with Content-Type in Header:
*$curl = curl_init();
$params="action=start_day_activity&woid=23";
$headers = [
'Content-Type: application/json',
"Authorization: Bearer $token"
];
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($curl);*
// Status 200
// $result becomes = "{"flag":0,"message":"No Request.","data":[]}"

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

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

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

Post status on Google Plus using PHP Curl

I have this code for logging into Google using Simple DOM Parser with curl. I've tried to post status on google plus using following code but unable to post on google plus
Any idea on how to solve this?
Here's my code for reference:
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "youremail#gmail.com",
"Passwd" => "yourpassword",
"service" => "writely",
"source" => "your application name"
);
// Initialize the curl object
$curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute
$response = curl_exec($curl);
// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$auth = $matches[1];
$params['newcontent'] = "Post on Google Plus Test By Me";
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
// Make the request
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($curl);
curl_close($curl);
Thanks For your help and co-operation
There is not a Google+ API to post statuses (unless you are a Google Apps user) and ClientLogin has been deprecated and will stop working soon. You're best bet is to look into using something like the share plugin.
In order not show the moved you have to set the agent header like firefox or something else
example
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl,"USER_AGENT","firefox my browser");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect
$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);`enter code here`
You need to add these two cURL params here too.
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect
Also, you are not outputting any response on your code
$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);

Categories