PHP - send file via curl with https - php

I am trying to send file via curl, but keep getting this error:
PHP Notice: Array to string conversion in /home/sasha/Documents/OffProjects/test/index.php on line 26
This is my code at the moment:
$target_url = 'https://address';
$file_name_with_full_path = realpath('mpthreetest.mp3');
$post = [
'textNote' => 'This is test',
'audioFile' => '#'.$file_name_with_full_path,
'department' => 'Test',
'timeDetected' => round(microtime(true) * 1000),
'subjectLine' => 'Test Test',
'recipients' => ['phone-number' => '111111111']
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
How can I make this working?

You can't have an array as a value, it's trying to convert the post data into a string. Check what the website you are posting to is expecting for "recipients", my guess would be some form of JSON string.

When you pass an array to CURLOPT_POSTFIELDS it will try to build a form-data content (see http://php.net/manual/en/function.curl-setopt.php for details).
But you have nested arrays here:
....
'recipients' => ['phone-number' => '111111111']
...
and this cannot work. You'll have to find a different way to pass multiple values as recipients, either as a JSON string as #fire mentioned, or with a serialized value, or whatever transformation you prefer.

Related

Upload file via REST API, IPBoard

Im trying to upload files via IPBoards REST Api but dont really know how to format the files.
This is how it looks right now but only gets error:
{
"errorCode": "1L296\/B",
"errorMessage": "NO_FILES"
}
Code:
$post = array(
'category' => 1,
'author' => 1,
'title' => 'Test title',
'description' => 'test description',
'files' => "{'test.txt':'".file_get_contents('/home/test/test.txt')."'}",
);
$target_url = 'https://example.com/api/downloads/files';
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 86400);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD,$apikey.":");
$result = curl_exec ($ch);
curl_close ($ch);
Here is the api documentation: API doc
Try using the http_build_query method and change the $post array slightly:
'files' => array($filename => $contents),
And change to:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

Could not parse the 'params' argument as valid JSON (PHP, cURL, REST)

After executing the PHP Script, I am getting the following error – Any help is appreciated. Based on my understanding, I created an array based on the bugzilla documentation - Thanks in advance
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/bug.html#create-bug
{"code":32000,"error":true,"message":"Could not parse the 'params' argument as valid JSON. Error: malformed number (no digits after initial minus), at character offset 1
$url ="http://localhost:8080/bugzilla/rest/bug";
$data = array(
"product" => "TestProduct",
"component" => "TestComponent",
"version" => "unspecified",
"summary" => "'This is a test bug - please disregard",
"alias" => "SomeAlias",
"op_sys" => "All",
"priority" => "P1",
"rep_platform" => "All"
);
$str_data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: application/json", "Accept: application/json"));
$username = 'ashish.sureka#in.abb.com';
$password = 'incrc';
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
$result = curl_exec($ch);
curl_close($ch);
echo $result
You have this:
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
which sends your array of data as a conventional key=value form submission, when you should have
curl_setopt($ch, CURLOPT_POSTFIELDS, $str_data);
^^^^^
which would send your JSON-encoded array instead.
You send $data in CURLOPT_POSTFIELDS but I think you need to give it $str_data which is encode in JSON
I think you have to replace
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
with
curl_setopt($ch, CURLOPT_POSTFIELDS, $str_data);
to transfer the json encoded array instead of the raw php array.

Todoist API - convert output from a string to array

Good evening, I have an account on Todoist. I would use the Todoist API to get all the projects. I wrote the following code:
$url = "https://todoist.com/API/v6/sync";
$post_data = array(
'token' => "12345678901234567890abcdefabcdef01234567",
'seq_no' => "0",
'resource_types' => '["projects"]'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
print_r($output);
curl_close($ch);
The output is a string like this:
{"TempIdMapping":{},"seq_no_global":6201059540,"seq_no":6201059540,"UserId":7179424,"Projects":[{"user_id":7179424,"name":"Project1","color":1,"is_deleted":0,"collapsed":0,"id":165361294,"archived_date":null,"item_order":1,"indent":1,"archived_timestamp":0,"shared":false,"is_archived":0},{"indent":1,"name":"Inbox","user_id":7179424,"color":7,"is_deleted":0,"collapsed":0,"inbox_project":true,"archived_date":null,"item_order":0,"is_archived":0,"archived_timestamp":0,"shared":false,"id":165339673}]}
Is there a way to convert this output into an array?
Example:
TempIdMapping => {},
seq_no_global => 6201059540,
seq_no => 6201059540
and so on...
Your output is JSON formated, so you can use json_decode like so
json_decode($output, true);
The second parameter convert the result to an associative array instead of an stdObject

Php Curl add string file as a post field

I am using mailGun web API and ran into the issue of adding inline files.
Our software creates an image and passes it around as a string. I want to inline that image
The problem that I have is that php curl takes in a file pointer, and not an actual file. I want to avoid writing a tmp file if possible as we have many process that work on the server and would not want to send a bad email
Thanks in advance
MailGun inline Sample:http://documentation.mailgun.net/user_manual.html#inline-image
Code sample that I am using:
function send_inline_image($image) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/samples.mailgun.org/messages');
curl_setopt($ch,
CURLOPT_POSTFIELDS,
array('from' => 'Excited User <me#samples.mailgun.org>',
'to' => 'sergeyo#profista.com',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomness!',
'html' => '<html>Inline image: <img src="cid:test.jpg"></html>',
'inline' => $image))
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$image = 'Some image string that we have generated'
send_inline_image($image)
You need to change only the inline parameter of array. I have done it and its works. Inline parameter should be an array instead of string image path. You can do it like this:
function send_inline_image($image) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/samples.mailgun.org/messages');
curl_setopt($ch,
CURLOPT_POSTFIELDS,
array('from' => 'Excited User <me#samples.mailgun.org>',
'to' => 'sergeyo#profista.com',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomness!',
'html' => '<html>Inline image: <img src="cid:test.jpg"></html>',
'inline' => array($image)//Use array instead of $image
))
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$image = 'Some image string that we have generated'
send_inline_image($image)
Look at comment "Use array instead of $image"

getting Bad Request during opening a querystring file in CURL

$URL:https://demo.firstach.com/https/TransRequest.asp?Login_ID=someit&Transaction_Key=somekey&Customer_ID=23&Customer_Name=Muhammad Naeem&Customer_Address=Address&Customer_City=city&Customer_State=HI&Customer_Zip=54000&Customer_Phone=--&Customer_Bank_ID=111111118&Customer_Bank_Account=12345678901234567890&Account_Type=Business Checking&Transaction_Type=Debit&Frequency=Once&Number_of_Payments=1&Effective_Date=12%2F05%2F2010&Amount_per_Transaction=10.00&Check_No=&Memo=&SECCType=WEB
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 0); // times out after Ns
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch); // run the whole process
print_r($result);
curl_close($ch);
i also used file_get_conent and fopen but all are returning me BAD REQUEST error,
please help me out
for more detail please see the link below
http://www.uqwibble.com/Phase-2/ach.php
Well assumign the code you posted is accurate then this line is the issue:
$URL:https://demo.firstach.com/https/TransRequest.asp?Login_ID=someit&Transaction_Key=somekey&Customer_ID=23&Customer_Name=Muhammad Naeem&Customer_Address=Address&Customer_City=city&Customer_State=HI&Customer_Zip=54000&Customer_Phone=--&Customer_Bank_ID=111111118&Customer_Bank_Account=12345678901234567890&Account_Type=Business Checking&Transaction_Type=Debit&Frequency=Once&Number_of_Payments=1&Effective_Date=12%2F05%2F2010&Amount_per_Transaction=10.00&Check_No=&Memo=&SECCType=WEB
here it looks liek you attemtp to define $URL but when you use it with cURL you are referencing $url. The varibales are case sensitive. Secondly you have $URL: which is not valid you want to use $url =.
Addiitonally i would encode the params like this:
$baseurl = 'https://demo.firstach.com/https/TransRequest.asp';
$params = array(
'Login_ID' => 'someit',
'Transaction_Key' => 'somekey',
'Customer_ID'= => 23,
'Customer_Name' => 'Muhammad Naeem',
'Customer_Address' => 'Address',
'Customer_City' => 'city',
'Customer_State' => 'HI',
'Customer_Zip' => '54000',
'Customer_Phone' => '--',
'Customer_Bank_ID' => '111111118'
'Customer_Bank_Account' => '12345678901234567890'
'Account_Type' => 'Business Checking'
'Transaction_Type' => 'Debit'
'Frequency' => 'Once'
'Number_of_Payments' => 1,
'Effective_Date'=> '12/05/2010',
'Amount_per_Transaction' => '10.00',
'Check_No'=> '',
'Memo'=> '',
'SECCType' => 'WEB'
);
$url = sprintf('%s?%s', $baseurl, http_build_query($params));
That way http_build_query will take care of all your url encoding and you can work with an array before hand so its easy to see whats going on and add/remove/change paramters. Alternatively if its a post request you could jsut use:
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
which will take care of all the parameter encoding and what not directly from the array this way they dont need to be appended manually to the $url.

Categories