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

后端 未结 4 2019
你的背包
你的背包 2021-01-02 16:02

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

thanks

相关标签:
4条回答
  • 2021-01-02 16:31

    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

    0 讨论(0)
  • 2021-01-02 16:39

    Hello Here Example Image upload

    <?php
    
    
    
    $path="aa/";// Set your path to image upload
    if(!is_dir($path)){
    mkdir($path);
    }
    $roomPhotoList = $_POST['image'];
    $random_digit=date('Y_m_d_h_i_s');
    $filename=$random_digit.'.jpg';
    $decoded=base64_decode($roomPhotoList);
    file_put_contents($path.$filename,$decoded);
    
    ?>
    

    it can be quick image upload code in php for ios and android

    0 讨论(0)
  • 2021-01-02 16:44

    You can debug the response of your php service and check the file upload from iphone using this app - http://itunes.apple.com/us/app/rest-client/id503860664?ls=1&mt=8 It was really helpful to debug serverside code.

    0 讨论(0)
  • 2021-01-02 16:50

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

    0 讨论(0)
提交回复
热议问题