Search code examples
phpauthenticationdynamiccurlzen-cart

Dynamically adding products to ZenCart via CURL


I've done a flash app that creates an high res image. I'm trying to dinamically add this image as a product to ZenCart, via a php script. I thought of accessing the admin via curl, adding a new product and, if successful, returning the newly created product ID.

This would be the login part of the script:

$curl = curl_init(); 
$fields = array('admin_name'=>'user', 'admin_pass'=>'pass');
$url = 'http://www.mystore.com/login.php';

curl_setopt($curl, CURLOPT_POST,        1); 
curl_setopt($curl, CURLOPT_URL,         $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,  1);                         
curl_setopt($curl, CURLOPT_POSTFIELDS,  $fields);

$result = curl_exec($curl);

if(curl_errno($curl)){ 
    echo 'error'; 
} else {
    echo $result;
}

but unfortunately I'm only getting the following error message:

Authorization Required

This server could not verify that you are authorized to access the document requested.     Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't     understand how to supply the credentials required.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I've tried using

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

with no success. Any ideas?

Thanks in advance


Solution

  • Try using different types of authentication. The manual mentions:

    CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM.

    So I would go for either curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

    or if that doesn't work or isn't supported try:

    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM);