I am trying to upload audio file using php I got this
i tried the same credentials in Postman and it works.
I can't find out what is wrong
I am following ACRCloud documentation here
HTTP/1.1 100 Continue
< HTTP/1.1 500 Internal Server Error
< Server: openresty/1.9.7.4
< Date: Mon, 23 Jan 2017 16:51:29 GMT
< Content-Type: application/json; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/5.6.21
< X-Rate-Limit-Limit: 600
< X-Rate-Limit-Remaining: 599
< X-Rate-Limit-Reset: 0
HTTP error before end of send, stop sending
<
Closing connection 0`
mycode
$request_url = 'https://api.acrcloud.com/v1/audios';
$http_method = 'POST';
$http_uri = '/v1/audios';
$timestamp = time();
$signature_version = '1';
$account_access_key = '';
$account_access_secret = '';
$string_to_sign =
$http_method . "\n" .
$http_uri . "\n" .
$account_access_key . "\n" .
$signature_version . "\n" .
$timestamp;
$signature = base64_encode(hash_hmac("sha1", $string_to_sign, $account_access_secret, true));
$realpath = realpath('uploads/*****.mp3');
if(class_exists('\CURLFile'))
$cfile = new \CURLFile($realpath, "audio/mp3", basename($realpath));
else
$cfile = '#' . $realpath;
$fields = array(
'audio_id' => '30007',
'title' => 'aya number 19',
'audio_file' => $cfile,
'bucket_name' => 'whatever',
'data_type' => 'audio', // if you upload fingerprint file please set 'data_type'=>'fingerprint'
'custom_key[0]' => 'track_id',
);
$headerArray = array();
$headers = array(
'access-key' => $account_access_key,
'signature' => $signature,
'signature-version' => '1',
'timestamp' => $timestamp,
);
foreach( $headers as $n => $v ) {
$headerArr[] = $n .':' . $v;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
$result = curl_exec($ch);
if ($result === FALSE) {
printf("cUrl error (#%d): %s<br>\n", curl_errno($ch),
htmlspecialchars(curl_error($ch)));
}
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
dd($result);
curl_close($ch);
I found out :
1- You have to use local file
2- Curl take absolute file path
3- You have to add custom_value[0] after custom_key[0] if You use it
now it works thanks to support
hope that help someone
Related
I'm using PHP cURL in order to connect to Binance Pay API. I get the following error:
{"status":"FAIL","code":"400002","errorMessage":"Signature for this request is not valid."}
Here is my code:
// Generate nonce string
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$nonce = '';
for($i=1; $i <= 32; $i++)
{
$pos = mt_rand(0, strlen($chars) - 1);
$char = $chars[$pos];
$nonce .= $char;
}
$ch = curl_init();
$timestamp = round(microtime(true) * 1000);
// Request body
$request = array(
"merchantTradeNo" => "12485634875fJhdd56",
"totalFee" => 15,
"productDetail" => "productDetail",
"currency" => "BUSD",
"returnUrl" => "",
"tradeType" => "WEB",
"productType" => "productType",
"productName" => "ProductName"
);
$json_request = json_encode($request);
$payload = $timestamp."\n".$nonce."\n".$json_request."\n";
$signature = strtoupper(hash_hmac('SHA512',$payload,$binance_pay_secret));
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "BinancePay-Timestamp: $timestamp";
$headers[] = "BinancePay-Nonce: $nonce";
$headers[] = "BinancePay-Certificate-SN: $binance_pay";
$headers[] = "BinancePay-Signature: $signature";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://bpay.binanceapi.com/binancepay/openapi/order");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); }
curl_close ($ch);
Regarding the documentation, everything seems ok, but it's not, so there is something wrong. Any idea? Thanks!
In python (and most likely in PHP as well) json encode is slightly modifying the request json by adding spaces and/or escaping quotes. The Binance Pay API requires the json string to be exactly how it is in the documentation. That is, with no extra spaces.
You forgot to add this
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_request);
I'm using Prefer:return=representation to get specific fields while creating accounts
API URL
https://xxxxxx-xxxx-xxxxx-xxxx/api/data/v9.0/accounts?$select=accountid,name,telephone1.
Request
$params['firstname'] = $first_name;
$params['lastname'] = $last_name;
$authHeader = 'Authorization: Bearer ' . $access_token;
$headers = array (
$authHeader,
'Content-Type:application/json',
'Prefer:return=representation', //NEWLY ADDED TO GET SPECIFIC
FIELDS
'Accept:application/json;'
);
$data_string = json_encode ( $params );
$custom_vars [CURLOPT_VERBOSE] = true;
$custom_vars [CURLOPT_HEADER] = true;
customLogs('case_create.log',print_r($data_string,true),3);
$result = gUnifyCURLCall ( $url, 'POST', $data_string, $headers, true, false, $custom_vars );
function gUnifyCURLCall($url, $method_type = 'GET', $data_string = null, $header = null, $ssl_verify = true,
$tls_version = false, $custom_vars = array()) {
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, $method_type );
if ($data_string != null) {
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data_string );
}
if ($header != null) {
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
}
if(!empty($custom_vars)) {
foreach ( $custom_vars as $key=>$val) {
curl_setopt($ch, trim($key), 1);
}
}
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}
I want to get server error code and also the response, so i'm passing CURLOPT_VERBOSE:TRUE,CURLOPT_HEADER:TRUE in the header. It returns following response from MS Dynamics.
HTTP/1.1 201 Created
Cache-Control: no-cache
Allow: OPTIONS,GET,HEAD,POST
Content-Type: application/json; odata.metadata=minimal
Expires: -1
Server:
x-ms-service-request-id: 85aed055-abff-42b0-a9bd-a4ad617638b1
REQ_ID: 85aed055-abff-42b0-a9bd-a4ad617638b1
AuthActivityId: xxxxxxxxxxxxxxx
Preference-Applied: return=representation
x-ms-ratelimit-burst-remaining-xrm-requests: 5995
x-ms-ratelimit-time-remaining-xrm-requests: 1,199.79
OData-Version: 4.0
Public: OPTIONS,GET,HEAD,POST
Set-Cookie: ApplicationGatewayAffinity=69ed338020af0cda5b08cef8314523419f00ed630d46bde35fe61b31f28285cf;Path=/;Domain=xxxxxxxx.xxxxxxx.dynamics.com
Date: Wed, 10 Jul 2019 10:43:48 GMT
Content-Length: 244
{"#odata.context":"https://xxxxxxxxxx.xxxxx.dynamics.com/api/data/v9.0/$metadata#accounts(accountid,name,fax)/$entity","#odata.etag":"W/\"11660448\"","accountid":"xxxxxxxxxx-xxxx-xxxx-xxxxxxx","name":"xxxxxx","fax":"9500293527"}
)
previously we were only working with the response code in the header to get the entity_id,now MS Dynamics web api is updated ,so now we want the response code from header and also the response using PHP
I've some problem with the PHP script. So, I'm trying to connect to the FMI Server, but when I'm enter a valid credentials the server always return HTTP code 330, instead of 200 OK. Also when I'm trying with invalid credentials it's return 401 and it's okay, but why I've this problem only with VALID credentials?
What I'm tried?
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
But no luck :(
Here's my code:
<?php
$username = "callibra#yandex.ru"; //Valid login
$password = "callibra4App"; //Valid Password
class FMIWebApplication {
private $client = array(
"user-agent" => "FindMyiPhone/472.1 CFNetwork/711.1.12 Darwin/14.0.0",
"headers" => array(
"X-Apple-Realm-Support" => "1.0",
"X-Apple-Find-API-Ver" => "3.0",
"X-Apple-AuthScheme" => "UserIdGuest",
"X-Apple-I-MD-RINFO" => "17106176",
"Accept" => "*/*",
"Connection" => "keep-alive",
"Accept-Encoding" => "br, gzip, deflate",
"Accept-Language" => "en-us",
"X-Apple-I-TimeZone" => "GMT+2",
"X-Apple-I-Locale" => "en_US"
)
);
public $username;
public $password;
public $devices = array();
public function __construct($username, $password) {
$this->username = $username;
$this->password = $password;
$this->authenticate();
}
public function authenticate() {
$url = "https://fmipmobile.icloud.com/fmipservice/device/".$this->username."/initClient";
list($headers, $body) = $this->curlPOST($url, "", $this->username.":".$this->password);
/*
if ($headers["http_code"] == 200) {
return 200;
};
if ($headers["http_code"] == 401) {
return 401;
};
if ($headers["http_code"] == 403) {
return 403;
};*/
echo $headers["http_code"];
}
private function curlPOST($url, $body, $authentication = "") {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $this->client["user-agent"]);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
if (strlen($authentication) > 0) {
curl_setopt($ch, CURLOPT_USERPWD, $authentication);
}
$arrHeaders = array();
$arrHeaders["Content-Length"] = strlen($request);
foreach ($this->client["headers"] as $key=>$value) {
array_push($arrHeaders, $key.": ".$value);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeaders);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$responseBody = substr($response, $header_size);
$headers = array();
foreach (explode("\r\n", substr($response, 0, $header_size)) as $i => $line) {
if ($i === 0)
$headers['http_code'] = $info["http_code"];
else {
list ($key, $value) = explode(': ', $line);
if (strlen($key) > 0)
$headers[$key] = $value;
}
}
return array($headers, json_decode($responseBody, true));
}
}
$API = new FMIWebApplication($username, $password);
$API->authenticate();
?>
That's what I'm getting from the server:
HTTP/1.1 330 Server: AppleHttpServer/70a91026 Date: Thu, 07 Mar 2019 13:23:40 GMT Content-Length: 0 Connection: keep-alive X-Responding-Instance: fmipservice:34000504:mr23p40ic-ztdg08174101:7004:1903B41:738abffa X-Responding-Server: mr23p40ic-ztdg08174101_004 X-Responding-Partition: p40 X-Apple-MMe-Host: p40-fmipmobile.icloud.com X-Apple-MMe-Scope: 639524741 Strict-Transport-Security: max-age=31536000; includeSubDomains Set-Cookie: NSC_q40-gnjqtfswjdf=6ad0a3dee1e78d9bd168cb5a7ceafc289128c7a38269b4bddde70dac09e4e273e5f12331;path=/;secure;httponly via: icloudedge:mc10p01ic-zteu01141501:7401:18RC846:Manchester X-Apple-Request-UUID: 8d5dc207-f1f8-453f-8863-9e0d5ab3b58b access-control-expose-headers: X-Apple-Request-UUID access-control-expose-headers: Via 330
Im trying to upload an image to Smugmug using the Upload API v2.
I've been trying for quite a time but it wont work.
This is what i've came up with so far.
When i run the script the response is as follows:
HTTP/1.1 100 Continue HTTP/1.1 200 OK Cache-Control: private, no-store, no-cache, max-age=1, must-revalidate Content-Type: text/xml; charset=utf-8 Date: Tue, 07 Jul 2015 14:18:50 GMT Edge-Control: no-store Expires: Tue, 07 Jul 2015 14:18:50 GMT Server: nginx X-Powered-By: SmugMug/1.0 X-SmugMug-Hiring: How to love what you do: http://www.smugmug.com/jobs/ X-SmugMug-Values: 1/4 - Thrill your customers Content-Length: 38 Connection: keep-alive
But the file isn't uploaded..
Do you see anything wrong with the code?
include_once("oauth/OAuth.php");
include_once("constants.php");
$access_token = '***';
$access_token_secret = '***';
$consumer = new OAuthConsumer(API_KEY, API_KEY_SECRET);
$signature_method = new OAuthSignatureMethod_PLAINTEXT;
$token = new OAuthToken($access_token, $access_token_secret);
$req_token = OAuthRequest::from_consumer_and_token($consumer, $token, "POST", 'http://upload.smugmug.com');
$req_token->sign_request($signature_method, $consumer, $token);
$parameters = $req_token->get_parameters();
$path = 'image.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
finfo_close($finfo);
$header[] = 'Authorization: OAuth realm="http://upload.smugmug.com/",
oauth_consumer_key='.$parameters['oauth_consumer_key'].',
oauth_token='.$parameters['oauth_token'].',
oauth_signature_method='.$parameters['oauth_signature_method'].',
oauth_signature='.$parameters['oauth_signature'].',
oauth_timestamp='.time().',
oauth_nonce='.md5(time() . mt_rand()).',
oauth_version=1.0';
$body[] = 'Accept: application/json';
$body[] = 'X-Smug-Version: v2';
$body[] = 'X-Smug-ResponseType: JSON';
$body[] = 'X-Smug-AlbumUri: /api/v2/album/******';
$body[] = 'X-Smug-Filename: image.png';
$body[] = 'Content-MD5: '.$base64.'';
$body[] = 'Content-Length: '.filesize($path).'';
$body[] = 'Content-Type: '.$mime.'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://upload.smugmug.com/');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_error ($ch);
$result = curl_exec($ch);
print_r($result);
Using OAuth can be tricky, and the upload API for SmugMug is a little unique. It might be helpful to use one of the PHP wrappers out there rather than write your own code. Here are two, both of which have examples of how to upload images:
1.) phpSmug
2.) DFM Smug Wrapper
For example with the DFM Smug Wrapper, you can do the following (copied from the README file included with the wrapper):
<?php
require_once("dfm-smug-wrapper/dfm-smug-wrapper.php");
$f = new DFM_Smug(
"oauth_consumer_key=12345678",
"oauth_secret=some_secret",
"token_id=12345",
"token_secret=some_other_secret",
"app_name=My Cool App/1.0 (http://app.com)",
"api_ver=2.0"
);
$f->images_upload("AlbumID=123456", "File=/path/to/image.jpg");
?>
<?php
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$paste_data=""; if(isset($_POST["paste_code"])) { $paste_data = $_POST["paste_code"]; }
echo $paste_data;
$returned_content = get_data('http://pastebin.com/api_public.php/paste_code(paste_data)');
echo $returned_content;
?>
This is my php code . where $paste_data contains the data to be pasted in a new page . How do I paste it using the function paste_code(String) ?
The documentation says that you need to submit a POST request to
http://pastebin.com/api_public.php
and the only mandatory parameter is paste_code, of type string is the paste that you want to make.
On success a new pastebin URL will be returned.
Bare bone example:
$ch = curl_init("http://pastebin.com/api_public.php");
curl_setopt ($ch, CURLOPT_POST, true);
// A new paste with the string "hello there SO"
curl_setopt ($ch, CURLOPT_POSTFIELDS, "paste_code=hello there SO");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
$response = curl_exec($ch);
echo $response;
and on running I get:
> POST http://pastebin.com/api_public.php HTTP/1.1
Host: pastebin.com
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 25
Content-Type: application/x-www-form-urlencoded
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Mon, 13 Dec 2010 07:51:12 GMT
< Content-Type: text/plain
< Server: nginx/0.8.52
< Vary: Accept-Encoding
< X-Powered-By: PHP/5.3.4-dev
< Via: 1.1 apac-nc06 (NetCache NetApp/6.0.6)
<
http://pastebin.com/Lc7kAw8Z* Closing connection #0
Clearly the response has the URL http://pastebin.com/Lc7kAw8Z
Visit it and you'll see a new paste containing hello there SO
FYI for others looking at this "post 2013", the api_public.php POST has been discontinued.
For those who stumple upon this thread via seach, here is a code that works in 2013:
<?php
$data = 'Hello World!';
$apiKey = 'xxxxxxx'; // get it from pastebin.com
$postData = array(
'api_dev_key' => $apiKey, // your dev key
'api_option' => 'paste', // action to perform
'api_paste_code' => utf8_decode($data), // the paste text
'api_paste_private' => '1', // 0=public 1=unlisted 2=private
'api_paste_expire_date' => '1D', // paste expires in 1 day
);
$ch = curl_init('http://pastebin.com/api/api_post.php');
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query($postData),
CURLOPT_RETURNTRANSFER => 1,
));
$re = curl_exec($ch);
curl_close($ch);
$pasteId = end(explode('/', $re));
echo "Created new paste.\r\n Link:\t{$re}\r\n Raw:\t" . sprintf('http://pastebin.com/raw.php?i=%s', $pasteId) . "\r\n";