Using PUT method with PHP cUrl Library

后端 未结 3 2008
温柔的废话
温柔的废话 2020-12-13 04:25

I\'m able to run the following curl command (at the command line) successfully:

curl -XPOST --basic -u user:password -H accept:application/json -H Content-ty         


        
3条回答
  •  时光取名叫无心
    2020-12-13 05:23

    Hi all I got it working using this configuration:

    // Start curl
    $ch = curl_init();
    // URL for curl
    $url = "http://localhost/";
    
    // Clean up string
    $putString = stripslashes($query);
    // Put string into a temporary file
    $putData = tmpfile();
    // Write the string to the temporary file
    fwrite($putData, $putString);
    // Move back to the beginning of the file
    fseek($putData, 0);
    
    // Headers
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    // Binary transfer i.e. --data-BINARY
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    // Using a PUT method i.e. -XPUT
    curl_setopt($ch, CURLOPT_PUT, true);
    // Instead of POST fields use these settings
    curl_setopt($ch, CURLOPT_INFILE, $putData);
    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString));
    
    $output = curl_exec($ch);
    echo $output;
    
    // Close the file
    fclose($putData);
    // Stop curl
    curl_close($ch);
    

    :)

提交回复
热议问题