Python – POST data naar PHP script
Voorbeeld URLLIB
In dit voorbeeld laat ik zien hoe je met een Python script een POST kan maken naar een PHP script.
1 2 3 |
<?php echo "Hallo " . $_POST["naam"] . "!, Waarde: " . $_POST["waarde"]; ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import urllib2, urllib # Data gegevens. mijndata = [("naam", "Wereld"), ("waarde", "5.00")] mijndata = urllib.urlencode(mijndata) #LET OP: USER AGENT IS NODIG (anders volgt een 403 forbidden!) #headers = {"User-Agent": "Mozilla/5.0"} posturl = "https://webadres/post.php" verzoek = urllib2.Request(posturl, mijndata) verzoek.add_header("Content-type", "application/x-www-form-urlencoded") verzoek.add_header("User-Agent", "Mozilla/5.0") antwoord = urllib2.urlopen(verzoek).read() print antwoord |
Start het python script met: sudo python test.py
Output in de console:Â Hallo Wereld!, Waarde: 5.00
Voorbeeld REQUESTS
In dit voorbeeld laat ik zien hoe je met een Python script een POST kan maken naar een PHP script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import requests url = 'http://mijn.website.nl/script.php' data = { 'NAME': 'mijn nieuwe product', 'DESC': 'Super mooi product!', 'QUANTITY': '8', 'PRICE': '2.56', 'WEIGHT': '100' } headers = { 'User-Agent': 'Mozilla/5.0' } response = requests.post(url, data=data, headers=headers) print(response.text) |
In het volgende voorbeeld kan je een bestand uploaden
Het PHP bestand:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php if(isset($_FILES['image'])){ $errors= array(); $file_name = $_FILES['image']['name']; $file_size =$_FILES['image']['size']; $file_tmp =$_FILES['image']['tmp_name']; $file_type=$_FILES['image']['type']; $file_ext=strtolower(end(explode('.',$_FILES['image']['name']))); if(empty($errors)==true) { move_uploaded_file($file_tmp, $file_name); echo "Success"; } else { print_r($errors); } } ?> |
Het PYTHON bestand:
1 2 3 4 5 6 7 8 |
import requests url = 'http://mijn.website.nl/upload.php' files = {'image': open('mijnbestand.png', 'rb')} headers = { 'User-Agent': 'Mozilla/5.0' } response = requests.post(url, files=files, headers=headers) print(response.text) |
Een ander voorbeeld met meer checks in het PHP script.
Het PHP bestand:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 3000000) { //3MB echo "Sorry, your file is too large."; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?> |
Het PYHON bestand:
1 2 3 4 5 6 7 8 9 |
import requests url = 'http://mijn.website.nl/upload.php' files = {'fileToUpload': open('mijnbestand.png', 'rb')} headers = { 'User-Agent': 'Mozilla/5.0' } response = requests.post(url, files=files, headers=headers) print(response.text) |
Hieronder ene voorbeeld als je eventueel ook andere data wilt meezenden, by het aanmaken van een foldernaam en het bestand daarin plaatsen:
Het PHP script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?php if ($_REQUEST["PRODCODE"] == "") { echo "ERROR:GEEN Productcode 'PRODCODE' opgegeven (voorbeeld: MOD-0000-MSF32)"; exit; } $prodcode = $_REQUEST["PRODCODE"]; $target_dir = "../mijn.website.nl/image/catalog/"; mkdir($target_dir . $prodcode); $target_file = $target_dir . $prodcode . "/" . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 3000000) { //3MB echo "Sorry, your file is too large."; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?> |
Het PYTHON script:
1 2 3 4 5 6 7 8 9 |
import requests url = 'http://mijn.website.nl/image.php' files = {'fileToUpload': open('mijnbestand.png', 'rb')} data = {'PRODCODE': 'SCR-0001'} headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' } response = requests.post(url, data=data, files=files, headers=headers) print(response.text) |