Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send / get files via web-services in php

is this possible ? what is the correct way to send files ?

thanks

like image 244
Haim Evgi Avatar asked Nov 08 '09 13:11

Haim Evgi


People also ask

How can upload image using postman in PHP?

Use Postman In Postman, create a new PUT request and include your newly created API call in the request. On the Body tab, and select binary. Select the file you would like to upload. Click Send.


2 Answers

I don't if you want your webservice to upload/download files. Anyway you can use curl(http://fr.php.net/curl ) to upload/download file from other webserver.

To get some file uploaded to the webservice from the user it's pretty much the same as gettings it from a form, please use the superglobal variable:$_FILES (doc) to get upload files.

for uploading from php to a webservice

$fullflepath = 'C:\temp\test.jpg';
$upload_url = 'http://www.example.com/uploadtarget.php';
$params = array(
 'photo'=>"@$fullfilepath",
 'title'=>$title
);  

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
curl_close($ch);

the webservice to get a file

$uploads_dir = '/uploads';
foreach ($_FILES["photo"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["photo"]["tmp_name"][$key];
        $name = $_FILES["photo"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}

PS: sorry for some reason stackoverflow doesn't like to make a link of $_FILES ... so I have linked the superglobals page instead

like image 186
RageZ Avatar answered Oct 03 '22 05:10

RageZ


You use this php program based on nusoap : http://biclim.com/WSClients.action

like image 21
Asap Avatar answered Oct 03 '22 03:10

Asap