cURL image upload using multipart/form-data - image

I have been 1 month dealing with the following code to post image using cURL to a remote server.
function post_file($url)
{
$file_url = "6.jpg";
$eol = "\r\n";
$BOUNDARY = '111151173914518';
$BODY=""; //init my curl body
$BODY.= '-----------------------------'.$BOUNDARY. $eol;
$BODY .= 'Content-Disposition: form-data; name="input1"' . $eol . $eol;
$BODY .= "left" . $eol;
$BODY.= '-----------------------------'.$BOUNDARY. $eol;
$BODY.= 'Content-Disposition: form-data; name="input2"' . $eol . $eol ;
$BODY .= "right" . $eol;
$BODY.= '-----------------------------'.$BOUNDARY. $eol;
$BODY.= 'Content-Disposition: form-data; name="input3"' . $eol . $eol ;
$BODY .= "up" . $eol;
$BODY.= '-----------------------------'.$BOUNDARY. $eol;
$BODY.= 'Content-Disposition: form-data; name="input4"' . $eol . $eol ;
$BODY .= "down" . $eol;
$BODY.= '-----------------------------'.$BOUNDARY. $eol;
$BODY.= 'Content-Disposition: form-data; name="input5"' . $eol . $eol ;
$BODY .= "center" . $eol;
$BODY.= '-----------------------------'.$BOUNDARY. $eol;
$BODY.= 'Content-Disposition: form-data; name="input6"' . $eol . $eol ;
$BODY .= "middle" . $eol;
$BODY.= '-----------------------------'.$BOUNDARY. $eol;
$BODY.= 'Content-Disposition: form-data; name="files[]"; filename="6.jpg"' . $eol;
$BODY.= 'Content-Type: application/octet-stream'. $eol; /
$BODY.= 'Content-Transfer-Encoding: multipart/form-data' . $eol . $eol;
$BODY.= file_get_contents($file_url) . $eol;
$BODY.= '-----------------------------'.$BOUNDARY .'--' . $eol. $eol;
$post_file = curl_init(); //init curl
curl_setopt($post_file, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data;boundary=---------------------------".$BOUNDARY) );
curl_setopt($post_file, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($post_file, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($post_file, CURLOPT_TIMEOUT, 40000);
curl_setopt($post_file, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($post_file, CURLOPT_URL, $url);
curl_setopt($post_file, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($post_file, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($post_file, CURLOPT_POST, TRUE);
curl_setopt($post_file, CURLOPT_POSTFIELDS, $BODY);}
It has been no issue for my input1 to input6, but there is no image has been uploaded.
But when upload using cURL, i get the following result:
REQUEST HEADERS:
POST /server.php HTTP/1.1
Host: MyServer.com
Accept: */*
Content-Type: multipart/form-data;boundary=-------------------------- -111151173914518
Content-Length: 42987
Expect: 100-continue
RESPONSE:
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: Microsoft-IIS/8.5
X-Powered-By: PHP/5.6.10
X-Powered-By: ASP.NET
Date: Mon, 31 Aug 2015 07:12:02 GMT
Content-Length: 171
array (
'input1' => 'left',
'input2' => 'right',
'input3' => 'up',
'input4' => 'down',
'input5' => 'center',
'input6' => 'middle',
)
All inputs are there except the image
I have tried to change the content type from application/octet-stream to image/jpeg and also
content transfer encoding from multipart/form-data to binary and base64 but all not working.

Related

How to convert array value to non-object into PHP

I have submitted this data with the postman form-data so how can I convert to non-object JSON from from-data array?
I have to try json_encode and json_decode but can't get json or non-object result into the controller.
In CodeIgniter controller.
Array
(
[------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition:_form-data;_name] => "first_name"
Janak
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="last_name"
Kumar
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="mobile_no"
123456789
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="country_code"
91
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="email_id"
test.mail#gmail.com
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="password"
123456
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="device_token"
123HFDT3434
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="device_type"
2
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="agency_name"
google
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
)
Is this what you're after?
<?php
$content = <<<OUT
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="last_name"
Kumar
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="mobile_no"
123456789
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="country_code"
91
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="email_id"
test.mail#gmail.com'
OUT;
$clean = str_replace("\n",'',$content);
$parts = array_filter(explode('------',$clean));
$array = array();
foreach ($parts as $row){
$parts2 = explode('name="',$row);
$parts3 = explode('"',$parts2[1]);
$array[$parts3[0]] = $parts3[1];
}
echo json_encode($array);
?>
I have a problem with your Array presentation as "first_name" would in fact be the value so I'm not sure if you've outputted your data correctly but let me know.

how to send codeigniter cart data on email

hello guys I tried to much to send codeigniter cart data on mail, it woking but I need to send it on html table.plz help me
if($cart = $this->cart->contents()){
foreach ($cart as $item){
$htmlContent=array(
'orderid'=>$user_id,
"product name": ".$item['name'],
);
$dataset[] = implode(', ', $htmlContent);
}
}
$content = implode("\n<br>", $dataset);
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= 'From:attaulahk782#gmail.com'."\n";
mail('attaullahk782#gmail.com', $subject, $content , $headers);
Please follow bellow code:
<?php
$content = '<table cellpadding="6" cellspacing="1" style="width:100%" border="0">';
$content .= '<tr>';
$content .= '<th>QTY</th>';
$content .= '<th>Item Description</th>';
$content .= '<th style="text-align:right">Item Price</th>';
$content .= '<th style="text-align:right">Sub-Total</th>';
$content .= '</tr>';
$i = 1;
foreach ($this->cart->contents() as $items):
$content .= '<tr>';
$content .= '<td>'.$items['qty'].'</td>';
$content .= '<td>'.$items['name'];
if ($this->cart->has_options($items['rowid']) == TRUE):
$content .= '<p>';
foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value):
$content .= '<strong>'.$option_name.':</strong>'.$option_value.'<br />';
endforeach;
$content .= '</p>';
endif;
$content .= '</td>';
$content .= '<td style="text-align:right">'.$this->cart->format_number($items['price']).'</td>';
$content .= '<td style="text-align:right">$'.$this->cart->format_number($items['subtotal']).'</td>';
$content .= '</tr>';
$i++;
endforeach;
$content .= '<tr>';
$content .= '<td colspan="2"> </td>';
$content .= '<td class="right"><strong>Total</strong></td>';
$content .= '<td class="right">$'.$this->cart->format_number($this->cart->total()).'</td>';
$content .= '</tr>';
$content .= '</table>';
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= 'From:attaulahk782#gmail.com'."\n";
mail('attaullahk782#gmail.com', $subject, $content , $headers);
?>

Cant get image preview of twitch streams

I want to show the preview images of streams, but I cannot get it work.
function get_url_contents($url){
$crl = curl_init();
curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false);
$timeout = 5;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
$url = "https://api.twitch.tv/kraken/search/streams?limit=25&offset=0&q=Age%20of%20Empires%20II:%20The%20Conquerors ";
$json = get_url_contents($url);
$json = json_decode($json);
foreach($json->streams as $stream) {
echo '<img alt="" style="float: left; width: 50px; padding-right: 7px;" src="' . $stream->channel->logo . '" />';
echo $stream->channel->status . '<br />';
echo $stream->channel->display_name . ' esta jugando ' . $stream->channel->game . '.<br />';
echo '<img alt="" src="' . $stream->preview . '" /> <br />';
echo 'Ver en vivo';
echo '<hr />';
}
?>
Im trying to show information of stream online, if i take out this sentece echo echo '<img alt="" src="' . $stream->preview . '" /> <br />'; everything work good, but if i put, it doesnt work . and this error show Catchable fatal error: Object of class stdClass could not be converted to string in on line 25 , the line 25 is the sentence i put.

how to get onchange value after select navigation menu in magento?

hi all how to get onchange value after select navigation menu in magento?
i trying to following code in topmenu.phtmlbut it's not working proper showing page is not found
But it's not working properly. What do i do?
you can try something like this
<select id="nav" class="nav" onchange="if (this.value) window.location.href=this.value">
Update Topmenu.php code with below code.
Remove this:
$html .= '<option ' . $this->_getRenderedMenuItemAttributes($child) . '>';
$html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>' . $this->escapeHtml($child->getName()) . '</span></a>';
if ($child->hasChildren()) {
if (!empty($childrenWrapClass)) {
$html .= '<div class="' . $childrenWrapClass . '">';
}
$html .= '<ul class="level' . $childLevel . '">';
$html .= $this->_getHtml($child, $childrenWrapClass);
$html .= '</ul>';
if (!empty($childrenWrapClass)) {
$html .= '</div>';
}
}
$html .= '</option>';
Add below code:
$html .= '<option value="' . $child->getUrl() . '" '>'. $this->escapeHtml($child->getName()) . '</option>';
if ($child->hasChildren()) {
$html .= $this->_getHtml($child, $childrenWrapClass);
}
And update select onchange code with below code:
onchange="if (this.value) window.location.href=this.value"

Checkbox ajax form posts the word array to email

I am trying to make a booking form with checkboxes using an existing contact form.js script and mailhandler.php from template monster. All of the text and textarea inputs work fine but the checkboxes only return the word array.
Here's parts of the code:
html
<form action="bin/MailHandler.php" method="post" id="booking-form" >
<fieldset>
<div class="grid_12">
<div class="bookingformwrapper">
<div class="headerbar">WAXING</div>
intrested in:<br/>
<input type="checkbox" name="waxing[]" value="kidum_esek" id="wax1"/>aaa<br />
<input type="checkbox" name="waxing[]" value="mitug_esek" id="wax2"/>bbb<br />
<input type="checkbox" name="waxing[]" value="laikim" id="wax3"/>CCC<br />
<input type="checkbox" name="waxing[]" value="aher" id="wax4"/>DDD<br />
ajax jquery
,submitFu:function(){
var data = { 'waxing[]' : []};
$(":checked").each(function() {
data['waxing[]'].push($(this).val());
});
_.validateFu(_.labels)
if(!_.form.has('.'+_.invalidCl).length)
$.ajax({
type: "POST",
url:_.mailHandlerURL,
data:{
choice:'waxing[]',
name:_.getValFromLabel($('.name',_.form)),
email:_.getValFromLabel($('.email',_.form)),
phone:_.getValFromLabel($('.phone',_.form)),
fax:_.getValFromLabel($('.fax',_.form)),
state:_.getValFromLabel($('.state',_.form)),
message:_.getValFromLabel($('.message',_.form)),
message2:_.getValFromLabel($('.message2',_.form)),
datepicker:_.getValFromLabel($('#datepicker',_.form)),
owner_email:_.ownerEmail,
stripHTML:_.stripHTML
},
success: function(){
_.showFu()
}
})
},
php
<?php
$owner_email = $_POST["owner_email"];
$headers = 'From:' . $_POST["email"];
$subject = 'A message from your site visitor ' . $_POST["name"];
$messageBody = "";
if($POST['waxing[]'] !='nope'){
$messageBody .= '<p>Choice: ' . explode(",", $_POST['waxing[]']);
$messageBody .= '<br>' . "\n";
}
if($_POST['name']!='nope'){
$messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['email']!='nope'){
$messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}else{
$headers = '';
}
if($_POST['state']!='nope'){
$messageBody .= '<p>State: ' . $_POST['state'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['phone']!='nope'){
$messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['fax']!='nope'){
$messageBody .= '<p>Fax Number: ' . $_POST['fax'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['datepicker']!='nope'){
$messageBody .= '<p>Date: ' . $_POST['datepicker'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['message']!='nope'){
$messageBody .= '<p>Address: ' . $_POST['message'] . '</p>' . "\n";
}
if($_POST['message2']!='nope'){
$messageBody .= '<p>Notes: ' . $_POST['message2'] . '</p>' . "\n";
}
if($_POST["stripHTML"] == 'true'){
$messageBody = strip_tags($messageBody);
}
try{
if(!mail($owner_email, $subject, $messageBody, $headers)){
throw new Exception('mail failed');
}else{
echo 'mail sent';
}
}catch(Exception $e){
echo $e->getMessage() ."\n";
}
?>
This is what gets returned on the email:
Choice: Array
Visitor: Father Christmas
Email Address: father#christmas.com
Phone Number: 01234 567890
Date: 25th December
Address: North Pole
Notes: Mince Pies
Where it says Choice: Array, I need it to give me the values of the selected/checked checkboxes.

Resources