cUrl with json string and file upload - php

how do I have to "translate" the following curl command into a valid php curl function?
curl -X POST
-F "images_file=#fruitbowl.jpg"
-F parameters=%7B%22classifier_ids%22%3A%5B%22testtype_205919966%22%5D%2C%22threshold%22%3A0%7D
'https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key={key}&version=2016-05-20'"
It seems that I'm doing something wrong and I can't figure out the problem:
$method = 'POST'
$url = 'https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=<myApiKey>&version=2016-05-20'
$data = array(
array(<file-information>),
array(<json-string>),
)
$header = array(
'Content-Type: application/json',
'Content-Length: ' . strlen(<json-string>),
)
)
public function send($method, $url, $data = null, $header = null)
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
$postData = $this->renderPostData($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
}
break;
}
if($header) {
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
}
protected function renderPostData($data)
{
$postData = array();
foreach ($data as $file) {
if ($file['isFile']) {
if(pathinfo($file['path'], PATHINFO_EXTENSION) == 'zip'){
$postData[$file['name']] = new \CURLFile($file['path'], 'application/zip', $file['name']);
}
else {
$postData[$file['name']] = new \CURLFile($file['path'], 'application/octet-stream', $file['name']);
}
} else {
// this contains the json encoded string
$postData[$file['name']] = $file['path'];
}
}
return $postData;
}
I tried several variations and the Watson Visual Recognition API error is now:
{
"custom_classes": 0,
"images": [
{
"error": {
"description": "Invalid image data. Supported formats are JPG and PNG.",
"error_id": "input_error"
}
}
],
"images_processed": 1
}
before it was:
{
"error": {
"code": 400,
"description": "Invalid JSON content received. Unable to parse.",
"error_id": "parameter_error"
},
"images_processed": 0
}
Thank you for your help!

My issue was this line:
$postData[$file['name']] = new \CURLFile($file['path'], 'application/zip', $file['name']);
the last parameter is the $postname. So to fix this issue I had to change this line to:
$postData[$file['name']] = new \CURLFile($file['path'], mime_content_type($file['path']), basename($file['path']));
and it worked - after I also removed the wrong $header completely
:)

Related

Shopify GraphQL Error "Parse error on \":\" (COLON) at [2, 35]" With PHP

Greetings I have a problem with my GraphQL I don't know how to pass data to my GraphQL without getting
Error Message: "Parse error on ":" (COLON) at [2, 35]"
here is what I'm trying to pass product variant id data and get some response here is the example of what I'm trying to do and my function for graphql
$variantId = (isset($data->variantId) && !empty($data->variantId)) ? strip_tags($data->variantId) : "";
if(empty($variantId)){
$result['error'] = "Product id not specified!";
}
$query = array("query" => '{
productVariant(id: '. ($variantId) .') {
availableForSale
}
}');
$variants = shopify_gql_call($_SESSION['access_token'], $_SESSION['shop_name'], $query);
if( isset($variants['response']) && !empty($variants['response']) ){
$result[] = $variants['response'];
}else{
$result['error'] = "Variants not found!";
}
function shopify_gql_call($token, $shop, $query = array()) {
// Build URL
$url = "https://" . $shop . ".myshopify.com" . "/admin/api/".getenv('API_DATE')."/graphql.json";
// Configure cURL
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);
// curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_USERAGENT, 'My New Shopify App v.1');
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
// Setup headers
$request_headers[] = "";
$request_headers[] = "Content-Type: application/json";
if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($query));
curl_setopt($curl, CURLOPT_POST, true);
// Send request to Shopify and capture any errors
$response = curl_exec($curl);
$error_number = curl_errno($curl);
$error_message = curl_error($curl);
// Close cURL to be nice
curl_close($curl);
// Return an error is cURL has a problem
if ($error_number) {
return $error_message;
} else {
// No error, return Shopify's response by parsing out the body and the headers
$response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
// Convert headers into an array
$headers = array();
$header_data = explode("\n",$response[0]);
$headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
array_shift($header_data); // Remove status, we've already set it above
foreach($header_data as $part) {
$h = explode(":", $part, 2);
$headers[trim($h[0])] = trim($h[1]);
}
// Return headers and Shopify's response
return array('headers' => $headers, 'response' => $response[1]);
}
}
I strongly suggest the use of https://packagist.org/packages/shopify/shopify-api instead of implementing your own function/http requests.
Your query should be something like this
query anynamehere($id: ID!){
productVariant(id:$id){
availableForSale
}
}
and then you submit the ID as part of another entry of the array, check the example below:
$query = [
"query" =>
'query anynamehere($id: ID!){
productVariant(id:$id){
availableForSale
}
}',
"variables" => [
'id' => $variantId
]
];
You should never concatenate the values as part of the query string (unless you want to deal with a lot of injection issues). Check more info about variables here https://graphql.org/learn/queries/

How to use AZURE face recognition Rest API?

I am using Face API with curl in PHP. But I am having issue when matching images.
I am able to generate faceId's but when matching I get different results than expected. I have two images belonges to same person but API indicates that these images are different. But when using Microsoft demo to compare images I get right result.
Here is microsoft demo link:
https://azure.microsoft.com/en-in/services/cognitive-services/face/#demo
Here are My images url
$img1 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/1645715403_1.jpg";
$img2 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/3.png";
Here is my code
<?php
function compare($image1, $image2)
{
$faceid = array();
$images = array($image1 , $image2);
$headers = ["Ocp-Apim-Subscription-Key: ********* ","Content-Type:application/json" ];
/* Getting faceId */
foreach($images as $data)
{
/* First step is to detect face */
$request_url='https://nexever.cognitiveservices.azure.com/face/v1.0/detect?detectionModel=detection_03&returnFaceId=true&returnFaceLandmarks=false';
/* Image to get faceid */
$detect = array('url' => $data);
$curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_URL, $request_url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($detect)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$strResponse = curl_exec($curl);
$curlErrno = curl_errno($curl);
if ($curlErrno) { $curlError = curl_error($curl);throw new Exception($curlError); }
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl);
$strResponse = json_decode($strResponse , true);
print_r($strResponse);
array_push($faceid , $strResponse[0]['faceId']);
}
// comparing by face ID
/* Match face url */
$request_url = 'https://nexever.cognitiveservices.azure.com/face/v1.0/verify';
/* Face ID to compare */
print_r($faceid);
$match = array("faceId1"=>$faceid[0], "faceId2"=>$faceid[1],"maxNumOfCandidatesReturned" =>10,"mode"=> "matchFace");
$curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_URL, $request_url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($match)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$strResponse = curl_exec($curl); $curlErrno = curl_errno($curl);
if ($curlErrno) {$curlError = curl_error($curl); throw new Exception($curlError); }
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return json_decode($strResponse, true);
}
$img1 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/1645715403_1.jpg";
$img2 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/3.png";
$ret = compare($img1, $img2);
//print_r($ret);
if(isset($ret['isIdentical']))
{
if($ret['isIdentical'] == 1)
{
echo "Same Person ";
}
else if($ret['isIdentical'] == 0)
{
echo "Different Person ";
}
}
?>
I have successfully got face id but unable to match. If I try some other images of same person it matches sometimes. The problem is result is not accurate.
but on microsoft demo it is working fine.
Pls try to use specify request param: recognitionModel=recognition_04 when you detect faces as official doc recommanded:
I modified your code as below, it works for me perfectly:
<?php
function compare($image1, $image2)
{
$faceid = array();
$images = array($image1 , $image2);
$faceAPIName = "nexever";
$apikey = "<your api key>";
$faceidAPIHost = "https://$faceAPIName.cognitiveservices.azure.com";
foreach($images as $data)
{
$detect = array('url' => $data);
$result = do_post("$faceidAPIHost/face/v1.0/detect?recognitionModel=recognition_04&detectionModel=detection_03",json_encode($detect),$apikey);
array_push($faceid , $result[0]['faceId']);
}
$request_url = "$faceidAPIHost/face/v1.0/verify";
/* Face ID to compare */
print_r($faceid);
$match = array("faceId1"=>$faceid[0], "faceId2"=>$faceid[1],"maxNumOfCandidatesReturned" =>10,"mode"=> "matchFace");
return do_post($request_url,json_encode($match),$apikey);
}
function do_post($url, $params,$key) {
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\nOcp-Apim-Subscription-Key: $key",
'method' => 'POST',
'content' => $params
)
);
$result = file_get_contents($url, false, stream_context_create($options));
return json_decode($result, true);
}
$img1 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/1645715403_1.jpg";
$img2 = "http://nexever.in/LibTravelSuperAdmin/images/temporary/3.png";
$ret = compare($img1, $img2);
//print_r($ret);
if(isset($ret['isIdentical']))
{
if($ret['isIdentical'] == 1)
{
echo "Same Person ";
}
else if($ret['isIdentical'] == 0)
{
echo "Different Person ";
}
}
?>
Result of your code:

FB messenger Bot not getting postback payloads

I am developing a facebook chatbot. I am facing a issue which I cannot solve. I am developing this in laravel. Here I cannot get the postback payload. here is my code
public function index(Request $request) {
if ($request->hub_verify_token === $this->verifyToken) {
echo $request->hub_challenge;
exit;
}
$input = json_decode(file_get_contents("php://input"), true);
$senderId = $input['entry'][0]['messaging'][0]['sender']['id'];
$messageText = $input['entry'][0]['messaging'][0]['message']['text'];
$pageId = $input['entry'][0]['id'];
$accessToken = "access_token_that_got_from_fb";
//set Message
if($messageText != "") {
$answer = "Howdy! User";
}
if($messageText == "hello") {
$answer = 'Testing hello from bot';
}
foreach ($input['entry'][0]['messaging'] as $message) {
// When bot receive message from user
if (!empty($message['postback'])) {
$answer = 'got it tada';
}
}
//send message to facebook bot
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ] //json_encode($request->getContent())
];
$ch = curl_init('https://graph.facebook.com/v2.11/me/messages?access_token='.$accessToken);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($ch);
curl_close($ch);
}
and my route is
Route::any('chatbot', 'ChatbotController#index');
here the message is working . but the postback payload request is not going to server. on the other hand using the same code in normal php file I am able to get postback paylod.
$hubVerifyToken = 'chatbot';
$accessToken = "access_token";
// check token at setup
if ($_REQUEST['hub_verify_token'] === $hubVerifyToken) {
echo $_REQUEST['hub_challenge'];
exit;
}
// handle bot's anwser
$input = json_decode(file_get_contents('php://input'), true);
$senderId = $input['entry'][0]['messaging'][0]['sender']['id'];
$messageText = $input['entry'][0]['messaging'][0]['message']['text'];
$postback = isset($input['entry'][0]['messaging'][0]['postback']['payload']) ? $input['entry'][0]['messaging'][0]['postback']['payload'] : '' ;
//set Message
if($messageText == "hi") {
$answer = "Hello";
}
if($messageText == "hello") {
$answer = "Hello there, welcome to Chatleads";
}
if($postback) {
$answer = "postback TADA";
}
//send message to facebook bot
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.11/me/messages?access_token=$accessToken");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
How to solve this issue? can anyone show me a path?
Solved it by checking at laravel.log file.
$this->messageText = isset($this->input['entry'][0]['messaging'][0]['message']['text']) ? $this->input['entry'][0]['messaging'][0]['message']['text'] : '' ;
messageText should be like this. that's why its causing an error.

PHP: Line running twice when a POST is called first

I have a few lines of code in a script, and if part A executes, then part B executes TWICE (that is: the cron.php file is added to the crontab TWICE). This is odd, because if I comment out part A, part B only executes ONCE.
Why is this happening? What am I missing?
// Part A
$url = "https://api.sendgrid.com/apiv2/customer.add.json";
$input = "api_user=$sendgrid_user_account&api_key=$sendgrid_master_password&username=$custname.domain.com&website=$sendgridsubuserdomain&password=$sendgridsubusersmtppass&con firm_password=$sendgridsubusersmtppass&first_name=$first&last_name=$last&address=$sendgridsubuseraddress&city=$sendgridsubusercity&state=$sendgridsubuserstate&zip=$ sendgridsubuserzip&email=$email&country=$sendgridsubusercountry&company=$custname&phone=$sendgridsubuserphone";
$sendgrid_output = postCurl($url, $input, false, false);
// Part B
chdir("/etc/nginx/");
exec("2>&1 ./addcron.sh $custname");
Supporting functions:
function postUrl($url, $data, $headers = null, $json_format = true) { // json format is only used if $data is also formatted as a string
$curl_result = postCurl($url, $data);
if($curl_result != false) return $curl_result;
$data_query = http_build_query($data);
$opts = array('http' => array('method' => 'POST', 'content' => $data_query));
if($headers) {
$opts['http']['header'] = $headers;
}
else {
$opts['http']['header'] = "Content-type: application/x-www-form-urlencoded";
}
$st = stream_context_create($opts);
$fp = fopen($url, 'rb', false, $st);
if(!$fp) {
//$result = http_post_fields($url, $data); TODO: add back in once AWS's PHP updated to include http_post_fields function
//if(!empty($result)) return $result;
}
$result = stream_get_contents($fp);
if(empty($result)) {
//$result = http_post_fields($url, $data);
//if(!empty($result)) return $result;
}
return false; // if all else fails, false
}
function postCurl($url, $values, $boolean_return = false, $json_format = true) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
if(is_array($values)) {
$values_string = "";
foreach($values as $key => $value) $values_string .= "$key=$value&";
$values_string = substr($values_string, 0, -1); // remove last "&"
}
else { // if it's not an array, assume JSON
$values_string = $values;
if($json_format == true) {
$input_array = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($values_string));
}
else {
$input_array = array('Content-Length: ' . strlen($values_string));
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $input_array);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $values_string);
// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// receive server response ...
if($boolean_return == false) curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
return $server_output;
}
The addcron.sh script:
#!/bin/bash
custname="$1"
(crontab -l; echo "* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php" ) | crontab -

Instagram API access rate limit information with specific PHP Class

I am using this PHP Class for Instagram's API: https://github.com/cosenary/Instagram-PHP-API.
It works perfectly but I can't seem to access the rate limit information for each call I make.
Inside the class, this is the method that makes the call:
protected function _makeCall($function, $auth = false, $params = null, $method = 'GET') {
if (false === $auth) {
$authMethod = '?client_id=' . $this->getApiKey();
} else {
if (true === isset($this->_accesstoken)) {
$authMethod = '?access_token=' . $this->getAccessToken();
} else {
throw new \Exception("Error: _makeCall() | $function - This method requires an authenticated users access token.");
}
}
if (isset($params) && is_array($params)) {
$paramString = '&' . http_build_query($params);
} else {
$paramString = null;
}
$apiCall = self::API_URL . $function . $authMethod . (('GET' === $method) ? $paramString : null);
$headerData = array('Accept: application/json');
if (true === $this->_signedheader && 'GET' !== $method) {
$headerData[] = 'X-Insta-Forwarded-For: ' . $this->_signHeader();
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiCall);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerData);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ('POST' === $method) {
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, ltrim($paramString, '&'));
} else if ('DELETE' === $method) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
$jsonData = curl_exec($ch);
if (false === $jsonData) {
throw new \Exception("Error: _makeCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
return json_decode($jsonData);
}
The information I need to access from Instagram's API is:
X-Ratelimit-Limit
X-Ratelimit-Remaining
(http://instagram.com/developer/limits/ for more information about Instagram's limits).
For obvious reasons I need the app I'm creating to "shut itself down" before the rate limit kicks in. By accessing the rate limit information I can achieve this.
I have found a Gist that should work with this class but I can't seem to get it to work: https://gist.github.com/cosenary/6af4cf4b509518169b88
Also this topic here on Stackoverflow seems to be fruitless:
Instagram API count limits using HTTP header
If anyone could help me out here that would be amazing!
Best regards,
Peter de Leeuw
I have modified the function _makeCall and it is as follows by adding first curl_setopt($ch, CURLOPT_HEADER, true); and calling the function processHeader() as cosenary suggested on this gist cosenary/ratelimit.php:
protected function _makeCall($function, $auth = false, $params = null, $method = 'GET') {
if (false === $auth) {
// if the call doesn't requires authentication
$authMethod = '?client_id=' . $this->getApiKey();
} else {
// if the call needs an authenticated user
if (true === isset($this->_accesstoken)) {
$authMethod = '?access_token=' . $this->getAccessToken();
} else {
throw new \Exception("Error: _makeCall() | $function - This method requires an authenticated users access token.");
}
}
if (isset($params) && is_array($params)) {
$paramString = '&' . http_build_query($params);
} else {
$paramString = null;
}
$apiCall = self::API_URL . $function . $authMethod . (('GET' === $method) ? $paramString : null);
// signed header of POST/DELETE requests
$headerData = array('Accept: application/json');
if (true === $this->_signedheader && 'GET' !== $method) {
$headerData[] = 'X-Insta-Forwarded-For: ' . $this->_signHeader();
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiCall);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerData);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ('POST' === $method) {
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, ltrim($paramString, '&'));
} else if ('DELETE' === $method) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
$jsonData = curl_exec($ch);
// split header from JSON data
// and assign each to a variable
list($headerContent, $jsonData) = explode("\r\n\r\n", $jsonData, 2);
// convert header content into an array
$headers = $this->processHeaders($headerContent);
// get the 'X-Ratelimit-Remaining' header value
$ratelimitRemaining = $headers['X-Ratelimit-Remaining'];
$this->setHeaderLimit($ratelimitRemaining);
if (false === $jsonData) {
throw new \Exception("Error: _makeCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
return json_decode($jsonData);
}
the processHeader() method which processes the header and the set and get methods for the $rateLimitRemaining are as follows :
private function processHeaders($headerContent){
$headers = array();
foreach (explode("\r\n", $headerContent) as $i => $line) {
if($i===0){
$headers['http_code'] = $line;
}else{
list($key,$value) = explode(':', $line);
$headers[$key] = $value;
}
}
return $headers;
}
private function setHeaderLimit($HeaderLimit){
$this->HeaderLimit = $HeaderLimit;
}
public function getHeaderLimit(){
return $this->HeaderLimit;
}
You can access the X-Ratelimit-Remaining now from another class just by calling the getHeaderLimit()
*Don't forget to declare the public field HeaderLimit within the class where _makeCall() resides, which in this case is Instagram.php.
**I have tested this solution and it works perfectly.
Hope this helps you guys :)

Categories