Php curl for Survey Monkey API - webhook - api

I have problems configuring a webhook for "Survey Monkey".
According to the API docs: Survey Monkey - webhook I think I have all the required arguments covered in my code and still I get the error "Invalid schema in the body provided". That mostly means there is something wrong with the POSTed JSON string data. But I can't find what is wrong...
my curl:
$surveyId = '123456789';
$data_string = array(
'name' => 'my webhook 1233456789 name',
'event_type' => 'response_completed',
'object_type' => 'survey',
'object_ids' => array($surveyId),
'subscription_url' => 'http://sometunnel.ngrok.io/job-survey-monkey-listener/completed',
);
$data_string = json_encode($data_string);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.surveymonkey.net/v3/webhooks');
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Authorization: bearer 123myaccestoken456.abc.def', 'Content-Length: ' . strlen($data_string)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$server_output = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($server_output, true)); die;
The dump of the return from Survey Monkey is:
array (size=1)
'error' =>
array (size=5)
'docs' => string 'https://developer.surveymonkey.com/api/v3/#error-codes' (length=54)
'message' => string 'Invalid schema in the body provided.' (length=36)
'id' => string '1002' (length=4)
'name' => string 'Bad Request' (length=11)
'http_status_code' => int 400
What do I do wrong here?

Thanks to General Kandalaft
Survey Monkey API does not accept integers for making a Curl to create a new webhook... You MUST send Id's as strings. So the above code in the question is correct.

Related

convert a curl request to php-curl - different responses received

I have this request to update a user using the user provisioning API in owncloud.
curl -X PUT https://example.com/ocs/v1.php/cloud/users/pinuccio -d 'key=email' -d 'value=jack#google.com' -H "OCS-APIRequest: true" -u 'admin:adminpwd'
I am trying to convert it to PHP CURL.
I ended up so far with this:
$username = 'admin';
$password = 'adminpwd';
$postData = array(
'key' => 'email',
'value' => 'jack#google.com'
);
$ch = curl_init('https://thesmartred.com/cloud/ocs/v1.php/cloud/users/'.$userid);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HEADER, "OCS-APIRequest: true");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
but I get a 997 response. While if I run this on shell I get a 405. Why do I get two different answers?
Anyone can help?
Here are couple of differences between your commandline request and php curl request.
First, You are using CURLOPT_HEADER in a wrong way. In php curl manual it says
CURLOPT_HEADER TRUE to include the header in the output
So to add a header in a proper way, example is:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'OCS-APIRequest: true'
));
Second, you are passing the array to your post fields. For array it does a multipart/form-data post. You need to pass data as string, and http_build_query() comes handy this case.
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));

Can not update product variant data in Shopify

I'm trying to update price and quantity of my product variant in Shopify using cURL and php below here my code:
$url = $api_url . '/admin/api/2020-01/variants/31941936382042.json';
$data_json = json_encode(
array(
'variants' => array(
'id' => 4574444978266,
'sku' => 'TEST-SKU-111',
'price' => 20,
'compare_at_price' => 20,
'cost' => 20, // This price will be coming form database
'inventory_quantity' => 10))
);
var_dump($data_json);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
var_dump($response);
curl_close($ch);
When I test it every time it says :
{"errors":{"variant":"Required parameter missing or invalid"}}
I checked official document https://shopify.dev/docs/admin-api/rest/reference/products/product-variant#update-2020-01 but no luck still. Can someone correct me what is the wrong I'm doing ?
Thanks in advance
Try to change the key for your data array, variants => variant.
Also, note the warning at the top of the page you're referring to. You should use Inventory APIs to manager your items inventory instead of setting up inventory_quantity directly.

cURL post with PHP failing

Using Postman, I have been able to successfully create an API call to retrieve information from the webserver.
However, I have been unsuccessful in doing the same with PHP. Following are a few screenshots from Postman which I am trying to replicate in PHP.
This is my PHP code -
$filters = array("1234", "28");
$selectors = array("Brand", "Name");
$body = array('Filter' => array('SKU'=>$filters, 'OutputSelector' => $selectors));
$url = "https://xyz.neto.com.au/do/WS/NetoAPI";
$method = "POST";
$headers = array(
"NETOAPI_ACTION: GetItem",
"NETOAPI_KEY: xxxxxxx",
"Accept: application/json",
"NETOAPI_USERNAME: puneeth"
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
P.S I have deliberately fudged the url and key.
I am trying to follow instructions as outlined here - https://developers.neto.com.au/documentation/engineers/api-documentation/products/getitem
Few things that might be preventing your code from working:
1. Array to string conversion
You have declared the body of the request as an array in $body. You need to JSON-encode it before passing it to cURL:
$body = array('Filter' => array('SKU'=>$filters, 'OutputSelector' => $selectors));
$bodyJSON = json_encode($body);
[...]
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyJSON);
2. HTTPS request
If you're getting an empty response, you'll also need to configure cURL for SSL communication. A quick fix for this is to ignore the remote server certificate validity:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Although this works, see this answer for a better solution.
3. Missing Content-Type
When sending raw postfields, some servers need to know what type of data you're sending. This is done by setting a Content-Type header:
$headers = array(
"Content-Type: application/json",
[...]
);

using Guzzle to access Wink API (Can't get it to work)

Thanks in advance for looking and any help/direction you can give me.
I am new to programming (and api's) and am trying to integrate with the Wink api. Documentation: http://docs.wink.apiary.io/
Using their examples I was able to accomplish oauth2 authentication and then query a listing of devices, etc. but can not get that to work with Guzzle (400 error) I am trying to use the CommerceGuys Guzzle Oauth2 plugin. https://github.com/commerceguys/guzzle-oauth2-plugin
Here is the example code that I can get to work:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://winkapi.quirky.com/oauth2/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"client_id\": \"consumer_key_goes_here\",\n \"client_secret\": \"consumer_secret_goes_here\",\n \"username\": \"user#example.com\",\n \"password\": \"password_goes_here\",\n \"grant_type\": \"password\"\n}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
I have tried many ways to use Guzzle to accomplish this but I always get a 400 (Bad Request) error. My code that I am trying to use is currently in my route for testing purposes:
use Guzzle\Http\Client;
use CommerceGuys\Guzzle\Plugin\Oauth2\Oauth2Plugin;
use CommerceGuys\Guzzle\Plugin\Oauth2\GrantType\PasswordCredentials;
use CommerceGuys\Guzzle\Plugin\Oauth2\GrantType\RefreshToken;
Route::get('wink', function(){
$oauth2Client = new Client('https://winkapi.quirky.com/oauth2/token');
$config = array(
'client_id' => 'xxxxxxxxxx',
'client_secret' => 'xxxxxxxxxxxxxxx',
'username' => 'kevin#xxxxxxxxxxs.com',
'password' => 'xxxxxxxx',
'grant_type' => 'password'
);
$grantType = new PasswordCredentials($oauth2Client, $config);
$refreshTokenGrantType = new RefreshToken($oauth2Client, $config);
$oauth2Plugin = new Oauth2Plugin($grantType, $refreshTokenGrantType);
$client = new Client('https://winkapi.quirky.com');
$client->addSubscriber($oauth2Plugin);
$response = $client->get('/users/me/wink_devices')->send();
var_dump($response);
});
Any help would be appreciated!!

How to get youku access_token

Where can I find the access_token, refresh_token in my youku account?
I found out how to upload video to youku.com using external script with the use of a API, but I need access_token, refresh_token to use it.
You need to authorize your Youku app and to use the get code to obtain a token.
Go to https://openapi.youku.com/v2/oauth2/authorize?client_id={YOURCLIENTID}&response_type=code&redirect_uri={YOURCALLBACKURL}.
Accept the authorization. You will be redirected to your callback URL. Be careful, it should be the same than the one you entered while creating your Youku application (same protocol too).
Use the get parameter code to get your access token by doing a POST CURL call to https://openapi.youku.com/v2/oauth2/token with the following parameters
if(isset($_GET['code']))
{
$url = "https://openapi.youku.com/v2/oauth2/token";
$params = array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"grant_type" => 'authorization_code',
"code" => $_GET['code'],
"redirect_uri" => $callback_url
);
$str_params = http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str_params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
echo $result;
}
The $result will be a json array containing the access_token {"access_token":"3cc08bffcd48a86a0e540f9ed1be42f4","expires_in":"2592000","refresh_token":"f8d78ce2005c9d1e0b62cd29f61ba3f9","token_type":"bearer"}
More infor here : http://open.youku.com/docs/docs?id=101
You can find the Youku api here: http://open.youku.com/docs/tech_doc.html
It is in Chinese, so I suggest you open this link using google chrome, then right click the page (after it is finished loading) and press 'Translate to English'
Hope this helps