![]() |
|
|
| 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 | |
| |
|
|
| There is no answer at this time. |
|
| 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 |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |