how to use dynamic CRM on premise api in php to create lead
<p>I wanted to create leads in dynamic CRM using api in php. Currently I am using rest api, using curl but it is throwing an error- </p>

<blockquote>
  <p>HTTP Error 401 - Unauthorized: Access is denied Status: 401NULL
  string(371) "HTTP/1.1 401 Unauthorized Cache-Control: private
  Transfer-Encoding: chunked...</p>
</blockquote>

<p>Please someone help me.</p>

<p>This is my code:</p>

<pre><code>    $ZDURL= "https://[org]/api/data/v8.1/accounts";

    $data =curlWrap($ZDURL); 
    var_dump($data);

    function curlWrap($ZDURL)
    {    
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
    curl_setopt($ch, CURLOPT_URL, $ZDURL);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

    $result = curl_exec($ch);
    if( $result === false) 
    {echo 'Curl error: ' . curl_error($ch); }
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    $output = curl_exec($ch);

    $status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
    echo "Status: ".$status;
    curl_close($ch);
    $decoded = json_decode($output);
    return $decoded;
}
</code></pre>