How do I post to a Google script with cURL in PHP and return text?

前端 未结 2 1564
闹比i
闹比i 2021-01-15 20:16

I\'m trying to do the bare minimum, just to get it working.

Here is my Google Script:

function doPost(e) {
  return ContentService.createTextOutput(J         


        
相关标签:
2条回答
  • 2021-01-15 20:49

    You need to look at your HTTP Request header to see what is actually being posted.

    When trouble shooting I add these options:

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT,10);
    curl_setopt($ch, CURLOPT_FAILONERROR,true);
    curl_setopt($ch, CURLOPT_ENCODING,"");
    
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    

    CURLINFO_HEADER_OUT will add "request_header" to curl_getinfo()

    You also want to look at these curl_getinfo() elements.

    • request_size
    • size_upload
    • upload_content_length
    • request_header
    0 讨论(0)
  • 2021-01-15 20:57

    Try reading the documentation for CURLOPT_POSTFIELDS and you'll see that is says To post a file, prepend a filename with @ and use the full path. That looks what you are trying to do. Note that in php 5.5, the CURLFile class was introduced to let you post files.

    If you are using php 5.5 or later, you might try this:

    $url = 'https://script.google.com/a/somedomain.com/macros/s/### script id ###/exec';
    
    // create a CURLFile object:
    $cfile = new CURLFile('file.pdf','application/pdf'); // you can also optionally use a third parameter
    
    // your POST data...you may need to add other data here like api keys and stuff
    $data = array("fileName" => $cfile);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    
    // FROM THE DOCS:
    // If value is an array, the Content-Type header will be set to multipart/form-data (so you might skip the line above)
    // As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix
    // As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    // set this to TRUE if you want curl_exec to retrieve the result
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $result = curl_exec($ch);
    if ($result === FALSE) {
        echo "The result is FALSE. There was a problem\n";
        $error  = curl_error($ch);
        var_dump($error);
        die();
    } else {
        echo "success!\n";
        var_dump($result);
    }
    
    // this can give you more information about your request
    $info = curl_getinfo($ch);
    if ($info === FALSE) {
        echo "curlinfo is FALSE! Something weird happened";
    }
    var_dump($info); // examine this output for clues
    

    EDIT: If you are not getting any error, and $result comes back with something like "Bad Request" then you will need to inspect the result more closely to find out what the problem is. A well-behaved API should have informative information to help you fix the problem. If the API doesn't tell you what you did wrong, you can examine the curlinfo you get from these commands:

    $info = curl_getinfo($ch);
    var_dump($info); // examine this output for clues
    

    if $result and $info don't tell you what you've done wrong, try reading the API documentation more closely. You might find a clue in there somewhere.

    If you can't figure out what the problem is using these tactics, there's not much else you can do with your code. You'll need more information from the maintainers of the API.

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