Link to home
Start Free TrialLog in
Avatar of Jason C. Levine
Jason C. LevineFlag for United States of America

asked on

Why is this curl operation failing?

I have the following PHP script that is attempting to post a file to another server using CURL.  

<?php

	$post_url = "https://xxx.xxx.org/cme_put.jsp";

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_URL, $post_url );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	
    $post_array = array(
        'my_file'=>'@/home/username/mysite.org/xxx/test.xml',
        "upload"=>"Upload"
    );

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
 
    $response = curl_exec($ch);

    echo "<pre>";
	print_r(curl_getinfo($ch));
	echo "</pre>";
    
    //echo curl_getinfo($ch);
	if (curl_error($ch))
		echo curl_error($ch);
	else
		echo $response;
		
	curl_close($ch);
?>

Open in new window


The response I get from the remote server is:

Array
(
    [url] => https://xxx.xxx.org/data/test.xml
    [content_type] => text/html; charset=iso-8859-1
    [http_code] => 404
    [header_size] => 515
    [request_size] => 397
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 1.059611
    [namelookup_time] => 2.3E-5
    [connect_time] => 0.07374
    [pretransfer_time] => 0.147874
    [size_upload] => 0
    [size_download] => 296
    [speed_download] => 279
    [speed_upload] => 0
    [download_content_length] => 296
    [upload_content_length] => 0
    [starttransfer_time] => 0.33763
    [redirect_time] => 0.721975
)

Not Found

The requested URL /data/test.xml was not found on this server.

Open in new window


However, if I do a normal form post with a file upload field to https://xxx.xxx.org/cme_put.jsp it works fine.

What would be causing curl to fail?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

[http_code] => 404

Script not found?
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason C. Levine

ASKER

I'm not sure, Ray.  The result is from the remote server so I have less of an ability to check on it.  I'm posting the array from the first script and echoing the response and it's complaining that the file we're trying to transfer isn't there.  

So curl is not doing what I think it's doing.  Why?
Let me work through the sample, then.
If you want to post the URL I would be glad to try it from my end.  If you want to obscure it a little bit, that's OK, just tell me the way to decode it.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey Jason - I need to see the page that has the form on it.  Can you show me that one, please?  Thanks, ~Ray
I will, but I'm away from the normal resources today.  Tomorrow.

Thanks to both of you for your help.
10-4.  I'll be here.  
Ray,

Form source attached:


<?php 
	$post_url = "https://somewhere.org/cme_put.jsp";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method="post" enctype="multipart/form-data" action="<?php echo $post_url ?>">
<input type="file" name="xmlFile" />
<input type="submit" name="btnSubmit" />
</form>


<?php // echo getcwd() ?>

</body>
</html>

Open in new window

Am I on firm ground if I assume that you have tested with the form, and it works perfectly every time?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray,

Yes, the form worked first time/every time.

We are investigating the possibility of the above.
And after all that time, it turned out to be a misconfig.  Sigh.  Thanks for the great advice and starter scripts, Ray.
You're welcome, and thanks for the points!  All the best, ~Ray