How to send multiple images into REST API - php

I'm taking multiple documents of candidate from database, put it into one array and sending this array and some other additional information to the REST API. Then it's throwing an exception FILE Type NOT Support. When sending single image it works properly.
foreach($a_files as $a_file)
{
$file_name_with_full_path=$a_file->uploaded_path.$a_file->document_name;
$mime_type=mime_content_type($file_name_with_full_path);
$cFile[] = '#'.realpath($file_name_with_full_path).';type='.$mime_type.';';
}
Sending this information through REST API. My post data is like:
$post= array('level' => "level",
'nameOfInstitute' => "nameOfInstitute",
'nameAsPerDocument' => "nameAsPerDocument",
'issueDate' => "issueDate",
'registrationNumber' => "registrationNumber",
'yearOfPassing' =>"yearofpassing",
'degree' =>"degree",
'fieldOfStudy' =>"fieldofstudy",
'grade' =>"grade",
'nameOfBoardUniversity'=>"university",
'file' =>$cFile
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_USERPWD, $val);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec ($ch);
$err = curl_error($ch);
curl_close($ch);
It's throwing an exception file type not supported. But when sending single file, it's working fine (without loop). What is the problem in above code when sending multiple files in the API. Please help me out from this problem.

Related

Why isn't my XML API request accepted by the API?

I have been trying, for days, to send a simple XML request to an API. Even with the help of the tech support, absolutely nothing works. I still get an error telling me that my XML isn't well-formed or is invalid.
Here is my cURL request:
$data = array_merge([
'ssl_transaction_type' => "$transactionType",
'ssl_merchant_id' => $this->merchant_id,
'ssl_user_id' => $this->user_id,
'ssl_pin' => $this->pin
], $data);
$xml = new \SimpleXMLElement('<txn/>');
$data = array_flip($data);
array_walk_recursive($data, array ($xml, 'addChild'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getXMLUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/x-www-form-urlencoded'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("xmldata=" . explode(PHP_EOL, $xml->asXML())[1])));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
Why do I explode the XML? It's because I need to only get the part with the root, not the version, etc. according to the tech support.
I followed this: XML request is not well-formed or request is incomplete but it still doesn't work.

Cloudflare API issue put request ("code":9020,"message":"Invalid DNS record type")

I am having issue while using cloudflare APIv4. Trying to update a dns record and not sure why am I receiving following error:
           
{"success":false,"errors":[{"code":1004,"message":"DNS Validation Error","error_chain":[{"code":9020,"message":"Invalid DNS record type"}]}],"messages":[],"result":null}
Here is the PHP function:          
function updateCloudflareDNS($zone_id,$dns_id, $updatedata){
$updatedata = '[{"name":"**.****.com"},{"type":"A"},{"ttl":"1"},{"content":"8.8.8.8"},{"proxied":"true"}]';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/".$zone_id."/dns_records/".$dns_id);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'X-Auth-Email: **********',
'X-Auth-Key: ***********'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $updatedata);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Also, I am putting record type "A" correctly as mentioned on the Cloudflare API documentation
Could someone help me out with this issue?
Thanks
You're sending a payload of:
[{"name":"**.****.com"},{"type":"A"},{"ttl":"1"},{"content":"8.8.8.8"},{"proxied":"true"}]
Here's what the API expects:
{"type":"A", "name":"example.com", "content":"127.0.0.1", "ttl":120, "priority":10,"proxied":false}
And this is how you properly construct JSON in PHP:
$POST = json_encode(array(
"type" => "A",
"name" => "...",
...
));

how to send xml file to server backend using php? [duplicate]

This question already has answers here:
How to properly send and receive XML using curl?
(2 answers)
Closed 9 years ago.
I have fetched data from server using this code, `$server = 'LOCALHOST:9000';
$headers = array(
"Content-type: text/xml"
,"Content-length: ".strlen($requestXML)
,"Connection: close"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
if(curl_errno($ch)){
print curl_error($ch);
echo " something went wrong..... try later";
}else{
echo " request accepted";
print $data;
curl_close($ch);
}`
Now I have to do reverse, how to send the data into server using php? curl method is the only way or is there any other method to do the same. Give me some example.
Sending/Receiving using cURL is the exact same thing. cURL is based on sending data to an given URL and possibly receiving a response. Let's take a simple example of getting a user and sending a user.
Getting a user would be
$data = array(
'user_id' => 1
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.site.com/getUser.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
getUser.php does nothing more than search for a user and return the data found.
$response would possibly contain data of the user, perhaps just a text-response containing the name. A serialized PHP array, an XML response with a full user profile.. etc.
Inserting/Sending data
$data = array(
'user_name' => 'Joshua',
'user_email' => 'my#email.com'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.site.com/addUser.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
addUser.php performs some validation of the fields and if validated inserts a user in the database
$response in this case does not contain the userdata (however: it's a possibility), but more likely the $response contains a result. An ok textresponse, a json/xml response or perhaps a '200 OK' header response
It's all basically the same. There is no difference in getting/sending data using cURL. It's all based on sending a request and in most cases do something with the outcome.

Differences in using PHP Curl vs command line curl

I am integrating the Badgeville REST API with my PHP 5.3, curl 7.22 application.
The API documentation for BV all uses command line curl calls for their examples. When I run these examples they work fine.
When I attempt to do the same thing with the PHP Curl class I always get a 500 error from the BV server.
I have tried to do the synonomous functionality with the Advanced Rest Client extension in Chrome.
PHP Curl Example:
$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
if($this->getRequestType() == 'POST')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'user[name]' => 'Generic+Username',
'user[email]' => 'johndoe%40domainname.com'
);
);
}
$response = curl_exec($ch);
Rest Client Example:
URL:
http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json
POST
No Headers Payload:
user[name]=Generic+Username&user[email]=johndoe%40domainname.com
I have manually created the command line curl call and ran that with shell_exec(), but I would REALLY prefer not having to do that.
In my research I found a Drupal module and all the API calls are done through fsockopen() calls.
Is there some way to do successfully make Badgeville calls using the PHP Curl class?
As it turns out Badgeville has a 500 error when a curl request comes in that has headers set.
Error returning code:
$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
if($this->getRequestType() == 'POST')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'user[name]' => 'Generic+Username',
'user[email]' => 'johndoe%40domainname.com'
);
);
}
$response = curl_exec($ch);
Properly functioning code:
$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json');
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
if($this->getRequestType() == 'POST')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'user[name]' => 'Generic+Username',
'user[email]' => 'johndoe%40domainname.com'
);
);
}
$response = curl_exec($ch);
SMH

Send File to HTTPS URL using CURL and PHP

I am attempting to send a file to an Https URL with this code:
$file_to_upload = array('file_contents'=>'#'.$target_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSER, FALSE);
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'file='.$file_to_upload);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close ($ch);
echo " Server response: ".$result;
echo " Curl Error: ".$error;
But for some reason I'm getting this response:
Curl Error: Failed to open/read local data from file/application
Any advice would help thanks!
UPDATE: When I take out CURLOPT_UPLOAD, I get a response from the target server but it says that there was no file in the payload
You're passing a rather strange argument to CURLOPT_POSTFIELDS. Try something more like:
<?
$postfields = array('file' => '#' . $target_path);
// ...
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
?>
Also, you probably want CURLOPT_RETURNTRANSFER to be true, otherwise $result won't get the output, it'll instead be sent directly to the buffer/browser.
This example from php.net might be of use as well:
<?php
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '#/home/user/test.png');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
On top of coreward's answer:
According to how to upload file using curl with php, starting from php 5.5 you need to use curl_file_create($path) instead of "#$path".
Tested: it does work.
With the # way no file gets uploaded.

Categories