Google Answers Logo
View Question
 
Q: PHP Post Function (Python code sample) ( No Answer,   2 Comments )
Question  
Subject: PHP Post Function (Python code sample)
Category: Computers > Programming
Asked by: xemion-ga
List Price: $12.00
Posted: 03 Jan 2005 08:52 PST
Expires: 05 Feb 2005 16:45 PST
Question ID: 450967
I want to post a file to a URL with PHP.  What is the code for this?

Here is a Python sample of exactly I want to accomplish:

def postit(url, file):
    f = open(file, "r")
    s = f.read()
    ff = urllib.urlopen(url, s)
    print ff.read()
    ff.close()
    f.close

Thanks.

xemion-ga

Request for Question Clarification by answerguru-ga on 03 Jan 2005 10:35 PST
Hi xemion-ga,

Have you had a look at this yet? Before posting it as an answer, I
just wanted to make sure it wasn't something you had already seen:

http://www.php.net/manual/en/features.file-upload.php

answerguru-ga

Clarification of Question by xemion-ga on 03 Jan 2005 11:21 PST
Yes, I've seen it.  That deals with people uploading files to MY site.
 I want to send a file, not receive one.
Answer  
There is no answer at this time.

Comments  
Subject: Re: PHP Post Function (Python code sample)
From: vladimir-ga on 03 Jan 2005 12:35 PST
 
I'm not familiar with the urllib Python library, but if uploading a
file is what you need to do, you can do it using the CURL library
functions in PHP, like so:

<?

$ch = curl_init("http://www.example.com/whatever");

$post["file"] = "@filename.txt";       // "@" is treated specially
$post["arg1"] = "val1";                // any additional parameters
$post["arg2"] = "val2";

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

curl_exec($ch);

curl_close($ch);

?>

Vladimir
Subject: Re: PHP Post Function (Python code sample)
From: atasever-ga on 09 Jan 2005 07:34 PST
 
hi,

go to http://www.phpclasses.org/httpclient and download the httpclient class.

it has a example file (test_http_post.php)

or you can test it with the following scripts :

*************************************
receieve.php ( the script who recieves the file)

<?
    if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name']))
    {
        if (copy($HTTP_POST_FILES['userfile']['tmp_name'],'recievedfile.file'))
            echo "FILE IS RECIEVED size: ". $HTTP_POST_FILES['userfile']['size'];
        else
            echo "FILE IS RECIEVED. BUT COULD NOT COPY FILE";
    }
    else
        echo "No file is recieved";
?>
***************************************************

***************************************************
send.php 
<?php
	require("http.php");

	$http=new http_class;
	$http->timeout=0;
	$http->data_timeout=0;

        $url="http://domain/recieve.php";
        $file_to_be_posted=file_get_contents("line.gif");

	$error=$http->GetRequestArguments($url,$arguments);
	$arguments["RequestMethod"]="POST";
	$arguments["PostValues"]=array(
		"submit"=>"Send",
		"MAX_FILE_SIZE"=>"1000000"
	);
	$arguments["PostFiles"]=array(
		"userfile"=>array(
			"Data"=>$file_to_be_posted,
			"Name"=>"line.gif",
			"Content-Type"=>"automatic/name",
		)
	);

$http->Open($arguments);
http->SendRequest($arguments);
$http->ReadReplyHeaders($headers);
$http->ReadReplyBody($body,1000);
echo HtmlSpecialChars($body);
$http->Close();




***************************************************

regards,
Umut ATASEVER

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy