I tried integrating Azure ML API with PHP but unfortunately getting an error in response.
Updated: I have used request response API sending through json response
Below is the response obtained on executing PHP script:
array(1) { ["error"]=> array(3) { ["code"]=> string(11) "BadArgument"
["message"]=> string(26) "Invalid argument provided." ["details"]=> array(1)
{[0]=> array(2) { ["code"]=> string(18) "RequestBodyInvalid" ["message"]=>
string(68) "No request body provided or error in deserializing the request
body." } } } }
PHP Script:
$url = 'URL';
$api_key = 'API';
$data = array(
'Inputs'=> array(
'My Experiment Name'=> array(
"ColumnNames" => [['Column1'],
['Column2'],
['Column3'],
['Column4'],
['Column5'],
['Column6'],
['Column7']],
"Values" => [ ['Value1'],
['Value2'],
['Value3'],
['Value4'],
['Value5'],
['Value6'],
['Value7']]
),
),
'GlobalParameters' => new StdClass(),
);
$body = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$api_key, 'Accept: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = json_decode(curl_exec($ch), true);
//echo 'Curl error: ' . curl_error($ch);
curl_close($ch);
var_dump ($response);
I have followed few examples, still unable to crack it. Please let me know the solution for this.
According to the error information, I think the issue was caused by requesting the ML REST API without correct json body.
I suggest that you can refer to the article "Getting started with the Text Analytics APIs to detect sentiment, key phrases, topics and language" to correctly format your input rows in JSON as the request body and try again.
Hope it helps.
If you can update your question for specifying which ML REST API you used, I think it's very helpful for figuring out the issue.
Expect your update.
Related
I’m having problems creating a new contact in FreeAgent.
I’m using the Light PHP wrapper for the OAuth 2.0 protocol and have managed to successfully set up oAuth2. I can connect to FreeAgent and successfully retrieve a list of customers. So far, so good. The problem I’ve got is sending data back the other way, i.e. creating a new contact in FreeAgent.
Here’s some code I’ve tried:
require_once($_SERVER['DOCUMENT_ROOT'].'/OAuth2/Client.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/OAuth2/GrantType/IGrantType.php');
$client = new OAuth2\Client($clientid, $secret);
$params = json_encode(array('contact' => array('first-name' => $firstname_unencrypted, 'last-name' => $surname_unencrypted)));
$response = $client->fetch("/contacts", $params, "POST", array('User-Agent' => 'MyApp', 'Accept' => 'application/json', 'Content-type' => 'application/json'));
var_dump($response);
The var_dump returned shows:
array(3) {
["result"]=>
bool(false)
["code"]=>
int(0)
["content_type"]=>
bool(false)
}
I’m pretty sure I’m doing something silly. I’ve tried sending in XML instead of JSON. I’ve tried using just a single array of params rather than the array in an array I’ve got in the sample. I’m going round in circles!
If someone could give me a little nudge in the right direction, I’d be eternally grateful!
Sample code that worked:
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<contact>
<first-name>$firstname_unencrypted</first-name>
<last-name>$surname_unencrypted</last-name>
</contact>";
if ($sandbox || $demo)
$ch = curl_init('https://api.sandbox.freeagent.com/v2/contacts');
else
$ch = curl_init('https://api.freeagent.com/v2/contacts');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$freeagentaccesstoken,
'Content-Type: application/xml',
'Accept: application/json',
'User-Agent: MyApp',
'Content-Length: ' . strlen($xml))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result=json_decode(curl_exec($ch), true);
$freeagenturl=$result['contact']['url'];
curl_close($ch);
I am able to successfully create groups in PHP using Microsoft Graph v1.0.
However, I get the following error when trying to update the same group:
The request is currently not supported on the targeted entity set
How can I resolve this error?
Code
$url = "https://graph.microsoft.com/v1.0/groups/104e2256-6bfc-40e8-a357-fa76cc36eb64";
$body = "{'allowExternalSenders': false, 'visibility': 'Private'}";
$headers = array(
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
curl_close($ch);
$json = json_decode($output, true);
print var_dump($json);
Output
["error"]=> array(3) {
["code"]=> string(10) "BadRequest"
["message"]=> string(65) "The request is currently not supported on the targeted entity set"
["innerError"]=> array(2) {
["request-id"]=> string(36) "26a5413a-17cc-4de8-b97d-00ffbc080098"
["date"]=> string(19) "2019-01-04T17:03:05"
}
}
This is a known issue with Application permissions (i.e. scopes applied when using Client Credentials).
From the documentation:
Only a subset of the group API pertaining to core group administration and management support application and delegated permissions. All other members of the group API, including updating autoSubscribeNewMembers, support only delegated permissions. See known issues for examples.
From Known Issues:
Examples of group features that support only delegated permissions:
Group conversations, events, photo
External senders, accepted or rejected senders, group subscription
User favorites and unseen count
I'm getting the following messages when calling the sabre rest API.
array(3) { ["access_token"]=> string(252) "my_accesstoken_was_here" ["token_type"]=> string(6) "bearer" ["expires_in"]=> int(604800) }
array(5) { ["status"]=> string(12) "NotProcessed" ["type"]=> string(10) "Validation" ["errorCode"]=> string(31) "ERR.2SG.SEC.INVALID_CREDENTIALS" ["timeStamp"]=> string(29) "2016-10-17T07:06:40.053-05:00" ["message"]=> string(48) "Authentication failed due to invalid credentials" }
I followed the sabre documentation located here: https://developer.sabre.com/docs/read/rest_basics/authentication
I have no idea why I'm getting the invalid credentials message as I'm using the access token provided from the API call to sabre.
The following is my code. What is the issue?
<?php
// get access token
$client_id= base64_encode("V1:user:group:AA");
$client_secret = base64_encode("my_password");
$token = base64_encode($client_id.":".$client_secret);
$data='grant_type=client_credentials';
$headers = array(
'Authorization: Basic '.$token,
'Accept: */*',
'Content-Type: application/x-www-form-urlencoded'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://api.sabre.com/v2/auth/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
var_dump($resf = json_decode($res,1));
$access_token = $resf['access_token']; // token provided from sabre
$token_type = $resf['token_type'];
$expires_in_seconds = $resf['expires_in'];
// // END get access token
// now to get api data using the provided access token
$url = 'https://api.test.sabre.com/v1/shop/flights/fares?origin=jfk&destination=lax&lengthofstay=5&pointofsalecountry=US';
$headers2 = array(
'Authorization: bearer '.$access_token,
'protocol: HTTP 1.1 '
);
echo "<br><br>";
$ch2 = curl_init();
curl_setopt($ch2,CURLOPT_URL,$url);
curl_setopt($ch2,CURLOPT_HTTPHEADER,$headers2);
curl_setopt($ch2,CURLOPT_RETURNTRANSFER,1);
var_dump(json_decode(curl_exec($ch2),1));
curl_close($ch2);
?>
The problem is that you're requesting a PROD access token (api.sabre.com) but sending the subsequent call to the test environment (api.test.sabre.com). Bear in mind that PROD and TEST credentials and tokens are not interchangeable.
If you send the second call to api.sabre.com you should be fine.
Hope this helps.
Bruno.
I'm trying to parse a JSON answer from Redmine API and I don't know how to get to the parts of the array.
Here is the code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://login:password#redmine.server/redmine/issues.json?cf_2=12345');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$data = json_decode($response);
When I make a var_dump($data), the answer looks like this:
array(1) { [0]=> object(stdClass)#1853 (14) { ["id"]=> int(96) ["project"]=> object(stdClass)#1852 (2) { ["id"]=> int(68) ["name"]=> string(7) "Test.......
So, when I make a for loop, I would like to access the parts of the array:
foreach($data as $issues){
var_dump($issues["id"]);
}
And so on. Any idea on this?
Stupid me...
The culprit was here:
$data = json_decode($response);
Should be:
$data = json_decode($response,true);
Now I get a proper PHP array.
So i have a small PHP script to generate short links, it work but some times i got this error :
Undefined property: stdClass::$id","file":ShortLink.php","line":31
This is my script :
<?php
class ShortLink {
public static function generateShortLink($longUrl)
{
//This is the URL you want to shorten
$apiKey = 'MY_API_KEY';
//Get API key from : http://code.google.com/apis/console/
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
//change the response json string to object
$json = json_decode($response);
curl_close($curlObj);
return $json->id;
}
}
When i start worked with this script 6 or 7 months ago i hadn't this error but now i start get it and i have no idea why, so please if someone has any idea i will be very appreciative.
Update :
When i vardump my $json i get that :
{ ["domain"]=> string(11) "usageLimits" ["reason"]=> string(26) "userRateLimitExceededUnreg" ["message"]=> string(40) "User Rate Limit Exceeded. Please sign up" ["extendedHelp"]=> string(36) "https://code.google.com/apis/console" } } ["code"]=> int(403) ["message"]=> string(40) "User Rate Limit Exceeded. Please sign up" }}
So i wondered if Google limited the Google shorten service ?
class ShortLink {
public static function generateShortLink($longUrl)
{
//This is the URL you want to shorten
$apiKey = 'YOUR_SERVER_API_KEY';
//Get API key from : http://code.google.com/apis/console/
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
//change the response json string to object
$json = json_decode($response);
curl_close($curlObj);
if(!is_object($json))
{
return(false);
}
return $json->id;
}
}
$api = new ShortLink();
$shorturlid=$api->generateShortLink('http://avecsrthgdgnb.avcd');
echo $shorturlid;
are you using new console then enable URL Shortener API.
Sometimes because of network curl is not returned with response within 30 seconds (default time limit in php for a cmd to finish)
Try changing the time limit in php.ini or if that is not in your control or you do not want to modify it for all the php cmds try bool set_time_limit ( int $seconds ) before calling curl_exec
Update:
I see there is no id field in the json that is returned.
{ ["domain"]=> string(11) "usageLimits"
["reason"]=> string(26) "userRateLimitExceededUnreg"
["message"]=> string(40) "User Rate Limit Exceeded. Please sign up"
["extendedHelp"]=> string(36) "https://code.google.com/apis/console"
}
}
["code"]=> int(403)
["message"]=> string(40) "User Rate Limit Exceeded. Please sign up"
}
}
And if you closely see your User rate limit has been reached See here for details on limitation on using google apis. (You may try different IP i.e. different machine or different api key and same code may start working).
Hope this helps