Home > Enterprise >  Using Curl with PHP Command line to PHP
Using Curl with PHP Command line to PHP

Time:04-14

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);
} 

CodePudding user response:

-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.

unction 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);
} 
  • Related