My PHP app is set up so that users can save/upload files via a form. I'm trying to write a shell script (to be run by a cron job) which re-uses the same model methods, but for a file which is NOT submitted via a form. I'm currently using curl to fetch the file, but I'm open to other solutions.
Here is how I currently fetch the file:
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$photo_data = curl_exec($ch);
curl_close($ch);
This appears to return base64 encoded data...
Hâ6#Ý^¦Ó¾ÊiAM¸¤¨q±´°jîÍrÓÄ¥!dqh/J°ØÉ(þ}ZKóܧóµßÅÑ .JÍWE-uXؤu!¢
±IãJT 浯utÞfåã6MGâYØUL:ÑÆZS÷'5Óç;ªåÓÒâbmlLKsô«ÄT
±üåûü_ĦSSM"ºlDAº¦Ë]Xcç[)X©¬oÞÃîIöÁB]aR+%+å@_·]³
...
Here is the format of the post data which my app is expecting:
[Photo] => Array
(
[file] => Array
(
[name] => desert2.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/php65uUCk
[error] => 0
[size] => 96573
)
)
So my question is how can I make the curl
ed file data accessible to the script which normally handles data from the above post request?
http://php.net/manual/en/function.fsockopen.php
<?php
// cURL
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$photo_data = curl_exec($ch);
curl_close($ch);
// Create Image Array
$imageData = array(
"name"=>"IMAGE NAME",
"type"=>"image/jpeg",
"tmp_name"=>"TEMP NAME",
"error"=>0,
"size"=>str_len($photo_data),
"photo"=>$imageData // Add this line as there is nothing in the data structure given that will pass the image data
);
// URL
$url = parse_url("http://www.wherethisisgoing.com/page.php");
$host = $url["host"];
$path = $url["path"];
$httpData = http_build_query($imageData); // Converts array to URL Parameters
$socket = fopensock($host, 80, $errno, $error, 30); // 80 is the port and 30 is the timeout length
if($socket) {
fputs($socket, "POST " . $path . " HTTP/1.1
");
fputs($socket, "Host: " . $host . "
");
fputs($socket, "Referrer: PUT SENDING URL HERE
");
fputs($socket, "Content-type: application/x-www-form-urlencoded
");
fputs($socket, "Content-length " . strlen($imageData) . "
");
fputs($socket, "Connection: close
");
fputs($socket, $imageData);
while(feof($socket)) {
// Get Results
$result .= fgets($socket, 128); // 128 should be enough to receive the response from the script
}
} else {
return array(
"status"=>"error",
"error"=>$error . " (" . $errno . ")"
);
}
//Do some type of checking to verify the image was uploaded
fclose($socket);