Skip to content

Instantly share code, notes, and snippets.

@walihassanjafferi
Created February 19, 2016 18:24
Show Gist options
  • Save walihassanjafferi/036e04fade8f06e33b7a to your computer and use it in GitHub Desktop.
Save walihassanjafferi/036e04fade8f06e33b7a to your computer and use it in GitHub Desktop.
Websolr Upload CSV using Php CURL
<?php
function upload_websolr_script($url){
$url = 'https://index.websolr.com/solr/xxxxxxxxx/update/csv/?commit=true';
if(!empty($_FILES['csv_file']['name'])) {
$upload_dir = wp_upload_dir();
$result = move_uploaded_file($_FILES["csv_file"]["tmp_name"],$upload_dir['path'].'/'. $_FILES["csv_file"]["name"]);
if ($result == 1) echo "<p>Upload done .</p>";
$file = ( $upload_dir['path'].'/'. $_FILES["csv_file"]["name"] );
$header = array("Content-type:text/csv; charset=utf-8");
$post = file_get_contents($file);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
//curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
echo curl_exec($ch);
curl_close($ch);
// If $ch is successful
if ($ch) {
echo '<h3>Hip Hip Hurray .. It worked! </h3>';
}else {
echo '<h3>No .. It did not work .. just like this! </h3>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment