PHP Personality Insights - php

<?php
$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,"'xxx':'xxx'");
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);
?>
I want to use IBM Bluemix Personality Insights with php curl and I am getting this error: Undefined variable: data What am I missing? How should I set up this variable, how should I pass the text that I want to have analyzed?

In this php code what is $data variable you just assign to $post_args['body']. i think you are using in post filed so just try if you get somthing from url than
$post_args = array('body' => $_POST['data']) ;
or just set some value if you sending to the url
$data = 'Your real data which you want to send in url ' ; than use in array .

Related

CURLOPT_POST, the request fails

I want to consume a web service by post method, and I have these fields:
and I am sending the information in this way;
$post_fields = array('nombre' => 'uno', 'apellido' => 'dos');
PHP code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query( $post_fields ) );
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
But it does not bring me information. Does anyone know how to receive the data or what my error is?

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

No Response after API GET request Call in PHP

Ok, so I have been working on this app that fetches data from an API, i've been coding it in PHP, but I recently ran into an Error. I don't get any response
I got the script from an online blog, as I have no previous experience with cURL
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: xxxx',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
return $result;
$get_data = callAPI('GET', 'https://api.brawlapi.cf/v1/player?tag=Y2QPGG', false);
$response = json_decode($get_data, true);
$errors = $response['response']['errors'];
$data = $response['response']['data'][0];
The API i'm using is https://docs.brawlapi.cf/#/endpoints/player

Update data using PUT with CURL PHP

I m try to update some data on mongodb using cms api
on terminal i can update information like this
curl -X PUT -d name=12345a https://api1.MYWEBISITE.com/api/v1in/user/2039/?t=mytoken
now using PHP i have try so many ways and no one looks to work for me
tried like this
class Curl {
public function put($url, $data_string){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
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);
return $result;
}
}
$curl = new Curl;
$data_string = '{"name" : "name123"}';
$url = "https://api1.MYWEBSITE.com/api/v1in/user/2039/?t=mytoken";
echo $curl->put($url, $data_string);
also i tried like this
$data = array( "name" => '12344');
$url = "https://api1.mywebsite.com/api/v1in/user/2039/?t=mytoken";
$curl = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$response = json_decode($result);
var_dump($response);
curl_close($curl);
both php solutions dont works, nothing is updated, no error msg is showing
any help?

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

Categories