I want to send bulk messages using twillio notify API using CURL in php I'm trying below Code :
$data = [];
$data['ToBinding'] = array("binding_type"=>"sms", "address"=>"+12013318779");
$data['Body'] ="test";
$ch = curl_init("https://notify.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXX/Notifications");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_USERPWD,'XXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXX');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resultData = curl_exec($ch);
I think I'm doing something wrong with the CURLOPT_POSTFIELDS but I tried each and every thing to change that but every time I got the below response :
{"code": 20001,
"message": "At least one parameter among Identity, Tag, and ToBinding must be specified",
"more_info": "https://www.twilio.com/docs/errors/20001",
"status": 400}
Can you guys please help me out.
Thanks
Twilio developer evangelist here.
ToBinding requires the data to be encoded as JSON. Try the following:
$data['ToBinding'] = json_encode(array("binding_type"=>"sms", "address"=>"+12013318779"));
Hope that helps.
I'm developing a website in a server and the storage is in another server and I have to deal with that somehow. I nerve experience this case and I found that the solution is to use curl.
Kindly explain to me how to use Curl in detail from zero.
Update:
I use the following code to test if cURL is installed and enabled:
<?PHP
phpinfo();
$toCheckURL = "http://board/accSystem/webroot/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $toCheckURL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$data = curl_exec($ch);
curl_close($ch);
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if(!$data) {
echo "Domain could not be found";
} else {
switch($code) {
case '200':
echo "Page Found";
break;
case '401':
echo "Unauthorized";
break;
case '403':
echo "Forbidden";
break;
case '404':
echo "Page Not Found";
break;
case '500':
echo "Internal Server Error";
break;
}
}
?>
Here is the result:
And I got (Page found) message
Now I can use cURL without worry, right ?
Note:
Both servers are local
As a PHP developer, you may already be familiar with PHP's most handy file system function, fopen. The function opens a file stream and returns a resource which can then be passed to fread or fwrite to read or write data. Some people don't realize though that the file resource doesn't necessarily have to point to a location on the local machine.
Here's an example that transfers a file from the local server to an ftp server:
$file = "filename.jpg";
$dest = fopen("ftp://username:password#example.com/" . $file, "wb");
$src = file_get_contents($file);
fwrite($dest, $src, strlen($src));
fclose($dest);
A listing of different protocols that are supported can be found in Appendix M of the PHP manual. You may wish to use a protocol that employs some encryption mechanism such as FTPS or SSH depending on the network setup and the sensitivity of the information you’re moving.
The curl extension makes use of the Client URL Library (libcurl) to transfer files. The logic of implementing a curl solution generally follows as such: first initialize a session, set the desired transfer options, perform the transfer and then close the session.
Initializing the curl session is done with the curl_init function. The function returns a resource you can use with the other curl functions much as how a resource is obtained with fopen in the file system functions.
The upload destination and other aspects of the transfer session are set using curl_setopt which takes the curl resource, a predefined constant representing the setting and the option’s value.
Here's an example that transfers a file from the local host to a remote server using the HTTP protocol's PUT method:
$file = "testfile.txt";
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://example.com/putscript");
curl_setopt($c, CURLOPT_USERPWD, "username:password");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_PUT, true);
curl_setopt($c, CURLOPT_INFILESIZE, filesize($file));
$fp = fopen($file, "r");
curl_setopt($c, CURLOPT_INFILE, $fp);
curl_exec($c);
curl_close($c);
fclose($fp);
A list of valid options for curl can be found in the php documentation.
The ftp extension allows you to implement client access to ftp servers. Using ftp to transfer a file is probably overkill when options like the previous two available... ideally this extension would be best used more advanced functionality is needed.
A connection is made to the ftp server using ftp_connect. You authenticate your session with the ftp server using ftp_login by supplying it a username and password. The file is placed on the remote server using the ftp_put function. It accepts the name of the destination file name, the local source file name, and a predefined constant to specify the transfer mode: FTP_ASCII for plain text transfer or FTP_BINARY for a binary transfer. Once the transfer is complete, ftp_close is used to release the resource and terminate the ftp session.
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "username", "password");
ftp_put($ftp, "destfile.zip", "srcfile.zip", FTP_BINARY);
ftp_close($ftp);
We are using same use case like you. we have two server : Application server and Storage server.Where application servers contains methods and UI part and storage server is where we upload files.Following is what we are using for file upload , that is working properly :
1) You have to add one php file that contains following methods on your storage server :
<?php
switch ($_POST['calling_method']) {
case 'upload_file':
echo uploadFile();
break;
case 'delete':
return deleteFile();
break;
}
function uploadFile() {
$localFile = $_FILES['file']['tmp_name'];
if (!file_exists($_POST['destination'])) {
mkdir($_POST['destination'], 0777, true);
}
$destination = $_POST['destination'] . '/' . $_FILES['file']['name'];
if (isset($_POST['file_name'])) {
$destination = $_POST['destination'] . '/' . $_POST['file_name'];
}
$moved = move_uploaded_file($localFile, $destination);
if (isset($_POST['file_name'])) {
chmod($destination, 0777);
}
$result['message'] = $moved ? 'success' : 'fail';
echo json_encode($result);
}
function deleteFile() {
if (file_exists($_POST['file_to_be_deleted'])) {
$res = unlink($_POST['file_to_be_deleted']);
return $res;
}
return FALSE;
}
?>
2) On your application server.
Create a method that will pass $_FILES data to storage server.
$data = array(
'file' => new CURLFile($_FILES['file']['tmp_name'],$_FILES['file']['type'], $_FILES['file']['name']),
'destination' => 'destination path in which file will be uploaded',
'calling_method' => 'upload_file',
'file_name' => 'file name, you want to give when upload will completed'
);
**Note :CURLFile class will work if you have PHP version >= 5**
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, PATH_FOR_THAT_PHP_FILE_ON_STORAGE_SERVER_FILE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$msg = FALSE;
} else {
$msg = $response;
}
curl_close($ch);
echo $msg;
In this way using CURL it will call storage server file method and upload files and in same way you can call method to delete file.
Hope this will help.
You may google, there are lots of tutorials on how to work with curl. In your case you need to develop two parts on both servers:
reception form, which will await for file being uploaded (receiver)
curl upload script (sender)
The sender script could be alike:
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '#/path/to/file.txt'));
curl_setopt($ch, CURLOPT_URL, 'http://yoursecondserver.com/upload.php');
curl_exec($ch);
curl_close($ch);
How to receive file on the second server you may read up in the PHP manual, lots of examples http://php.net/manual/en/features.file-upload.php
Potentially you might want to use FTP connection, if have it installed on your servers, it might be even easier.
In this case You can call PHP script on remote server with path to file on local server.
Then on remote server using file_get_contents save the file.
Example on local server:
<?php
file_get_contents('http://somestaticfilesserver.com/download.php?file=path/to/some/file.png')
and download.php can look like this:
<?php
$remoteHost = 'http://serversendingfile.com';
$rootPath = realpath(__DIR__) . DIRECTORY_SEPARATOR . 'uploaded' . DIRECTORY_SEPARATOR;
$filePath = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, filter_input_array(INPUT_GET)['file']);
$fileName = (basename($filePath));
$remoteFile = $remoteHost . $filePath;
$file = file_get_contents($remoteFile);
$absolutePath = str_replace($fileName, '', $rootPath . $filePath);
if (!is_dir($absolutePath)) {
mkdir($absolutePath, 0777, true);
}
if (file_put_contents($absolutePath . $fileName, $file, FILE_APPEND)) {
die('ok');
}
die('nok');
the given path will be kept relative to $rootPath
First, make sure the other server will accept your connections, look up Cross Domain Policy issues. Also, make sure that the file is publicly available (ie: you can download it by navigation to the URL with a standard browser).
Once everything is setup, you can use file_get_contents to get the contents of the file and file_put_contents to save it locally:
file_put_contents('file_name', file_get_contents('FILE_URL'));
Your other questions (delete, edit) are really a different beast and should most likely be handled in their own questions, as there is no way to do this from your website server alone for obvious security reasons.
You would need to expose an API on the storage server and hit that API from the website to make the storage do actions the appropriate actions.
You can upload file on another server using posting the file.
$request = curl_init('http://example.com/');
// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => '#' . realpath('example.txt')
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
curl_close($request);
You can handle the Uploaded file using $_FILES['file']
I want to know how to get list of all active jira issues through rest api. Once when i get the list I am planning on using si/jira.issueviews:issue-xml feature in the uri to download all issues in xml format. But I do not have the list of all the issues. I have googled and only came up with answers that has to do with getting issues under a specific project.
This is how I do a search using cURL and the REST API in PHP:
$url = "http://YOUR_SERVER_URL_AND_PORT/rest/api/2/search";
$data = array("jql" => "status not in (Closed, Resolved, Done)","startAt" => 0,"maxResults" => 200);
$httpHeader = array(
'Accept: application/json',
'Content-Type: application/json'
);
try{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "USER_NAME:USER_PASS");
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error == '')
$out = $result;
else
$out = json_encode(array('error' => $ch_error));
}catch(Exception $e){
$out = json_encode(array('error' => $e->getMessage()));
}
curl_close($ch);
You need to change YOUR_SERVER_URL_AND_PORT, USER_NAME and USER_PASS with your own information.
NOTE: the previous REST query will only return issues that you are allowed to see. If you need to see ALL issues, you need to provide Jira Admin credentials in your cURL config.
Twitter request exceed problem. It shows requests exceed 150 per hour.
This is my code :
$trends_url = "https://api.twitter.com/1/statuses/friends.json?screen_name=".$_SESSION['Auth']['User']['screen_name'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $trends_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
I am using PHP curl to get Twitter friend list. If I manuaaly post this URL in browser like "https://api.twitter.com/1/statuses/friends.json?screen_name=Rock_Feather".
It works fine, but not giving data back while using curl.
Can someone tell me how can I get the feedburner subscribers count as text using php? I am trying the following, but it does not work as I think feedburner has changed the contents of the xml file.
Thanks for any help.
$whaturl="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=USERNAME";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $whaturl);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];
echo $fb;
Need to alter line 7 to:
$fb = $xml->feed->entry->attributes()->circulation;
//Much later edit
I actually have a couple of posts on my blog by now with code for fetching facebook / twitter / feedburner counts. Code with APC caching is here:http://www.glowingminds.co.uk/facebook-likes-twitter-follows-and-feedburner-count-in-php-with-curlsimplexml-deluxe/