0

This works from the command line how do I convert this to PHP I have tried anything but nothing seems to work.

curl -H "Authorization: Bearer APIKEY" https://www.gocanvas.com/apiv2/forms.xml -F '[email protected]'

Here is a function I have tried.

function getgocan(){
    $url = 'https://www.gocanvas.com/apiv2/[email protected]';

    $ch = curl_init();

    $jsonData = array(
        'text' => ''
    );
    $jsonDataEncoded = json_encode($jsonData, true);

    $header = array();
    $header[] = "APIKEY";

    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    $result = curl_exec($ch);
    curl_close($ch);

    var_dump($result);
} 
2
  • Why are you using json_encode()? The parameter in the CLI is not JSON. Commented Apr 13, 2022 at 17:36
  • Good question I had this code I have used in the past but I am really new to this so I assume I should not. Commented Apr 13, 2022 at 17:38

1 Answer 1

2

-F sends form fields in the POST data, not URL query parameters. So you need to put username=XXX into CURLOPT_POSTFIELDS, and it shouldn't be JSON.

You can give an associative array to CURLOPT_POSTFIELDS, and it will encode it automatically.

function getgocan(){
    $url = 'https://www.gocanvas.com/apiv2/forms.xml';

    $ch = curl_init();

    $params = array(
        'username' => '[email protected]'
    );

    $header = array(
        'Authorization: Bearer APIKEY'
    );

    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    $result = curl_exec($ch);
    curl_close($ch);

    var_dump($$result);
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.