cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

Curl posting to a php script

From: Brian Moynihan <brian.moynihan_at_3procure.com>
Date: Fri, 26 Sep 2003 15:14:30 +0100

Hi there,

We have a client who is using curl in a C program to HTTP POST us an xml file. On our side we have a php script to accept the file and save it to a folder on our server. To test this we created a C file of our own using curl.

Unfortunately this has not proved to be as easy as we had thought.

We know that the php script is not picking what should be sent by the C program. To make matters worse, we are now unsure if our C program is posting the file. We know it will post form parameters so I have included both C code and PHP script below.

Our C code is as follows:

  CURL *curl;
  CURLcode res;
  FILE *headerfile;
  FILE *contentfile;
  struct HttpPost *formpost=NULL;
  struct HttpPost *lastptr=NULL;
  struct curl_slist *headerlist=NULL;
  char error[2048];
  const char *url=argv[1];
  const char *invoice =argv[2];

  curl_global_init(CURL_GLOBAL_DEFAULT);
  curl = curl_easy_init();

  if(curl)
  {
    // Setup form to post file contents.
    AddFileToForm(&formpost, &lastptr, invoice);
    AddHeaders(&headerlist);

    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);

    curl_easy_setopt(curl, CURLOPT_WRITEHEADER, headerfile);
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

    res = curl_easy_perform(curl);
    
    curl_easy_cleanup(curl);
    curl_global_cleanup();
}

void AddHeaders(struct curl_slist **headerlist)
{
  *headerlist = curl_slist_append(*headerlist, "Accept: text/xml");
  *headerlist = curl_slist_append(*headerlist, "Content-type: text/xml");

  return;
}

void AddFileToForm(struct HttpPost **formpost,
                struct HttpPost **lastptr,
                const char *filename)
{
  curl_formadd(formpost, lastptr, CURLFORM_COPYNAME, "filename",
                CURLFORM_NAMELENGTH, 9,
                CURLFORM_FILE, filename,
                CURLFORM_CONTENTTYPE, "text/xml",
                CURLFORM_END);

  return;
}

Then our PHP looks like:

<?php

  foreach( $_FILES as $varname => $fileinfo ){
    $filename = $fileinfo["name"];
    $tmpname = $fileinfo["tmp_name"];
    $ftmp=fopen("brianLogs", "w+");
    fwrite($ftmp, "Filename - " . $fileinfo["name"] . "\nTemp Name - " . $fileinfo["tmp_name"]);
  }

  $uploaddir = '/usr/local/nameofdir;
  $uploadfile = $uploaddir. $_FILES['filename']['name'];

  if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {

    $fp=fopen("brianLog", "w+");
    $string = "HTTP POST of Purchase Order SUCCESSFUL.************ File Name: ".$_FILE['filename']['name']."\n";
    fwrite($fp, $string);

    //print_r($_FILES);
  }
  else {
    $fp=fopen("brianLog", "w+");
    $string = "HTTP POST Failed !************* File Name: ".$_FILES['filename']['name']."\nTmp Name: " .$_FILES['filename']['tmp_name'] . "\nArray Length : " . COUNT($_POST);
    fwrite($fp, $string);
  }
?>

We just don't know why it's not working? If anyone has any suggestions or idea's, we would openly welcome them.

Regards,
Brian.

-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Received on 2003-09-26