I am calling a CURL to send out SMS, which works fine. But after calling page goes blank and show like this. Below is my code and screen shot
Here is my code
if(isset($_POST['btnAddProduct']))
{
$msgUser = $_POST['txtSMS'];
$phone = "";
$fields_string = "";
foreach($_POST['check_list'] as $selected){
$phone .=$selected.",";
}
$phone = rtrim($phone,',');
$url = 'http://www.sms.com/PostSms.aspx';
$fields = array(
'userid' => urlencode('username'),
'pass' => urlencode('pwd'),
'phone' => urlencode($phone),
'msg' => urlencode($msgUser),
'title' => urlencode('code')
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//echo $fields_string;exit;
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
//$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
<script type="text/javascript">
alert("Message sent successfully");
window.location ="sms.php";
</script>
<?php
}
Related
My first.php file
$statusString = "u=10000;t1=1479409;s=10;r=-33;v=3.68;";
$macaddress= "10000";
$url = 'second.php';
$fields = array(
'newFormat'=>urlencode($statusString),
'MACAddress'=>urlencode($macaddress)
);
$fields_string = '';
foreach($fields as $key=>$value)
{
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
Here I am trying to send data from my first.php to my second.php
My second.php file
if (isset($_GET['newFormat']))
{
$str = $_GET['newFormat'];
}
else
$str = 'no data';
if (isset($_GET['MACAddress']))
{
$macAddress = $_GET['MACAddress'];
}
Here I am trying to retrieve the data sent by first.php.
My first.php invokes second.php, but second.php cannot retrieve any data. In my second.php, $str turn out to be 'no data' as in the the else part and $macAddress is empty! Can anyone please help me solve this?
In second.php, should be using _POST and not _GET
I just started looking at the RingCentral API
I am a little confused on how they expect the data.
I tried first with curl using:
$url = ' https://service.ringcentral.com/faxapi.asp';
$faxData = array();
$faxData['Username'] = 'xxxxxxxx';
$faxData['Password'] = 'xxxxxxxx';
$faxData['Recipient'] = $faxNumber.'|TEST';
$faxData['Attachment'] = ROOT_PATH.$fileLocation;
// build url encoded string
$fields_string='';
foreach($faxData as $key=>$value) {
$fields_string .= $key.'='.urlencode($value).'&';
}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($faxData));
curl_setopt($ch,CURLOPT_POSTFIELDS, $faxData);
//execute post
$result = curl_exec($ch);
$err = curl_errno ( $ch );
$errmsg = curl_error ( $ch );
$header = curl_getinfo ( $ch );
$httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
//close connection
curl_close($ch);
Then I tried sending as an email using the number#ringcentral.com and I still am unable to get this to work at all. Their support site is useless as I see many unanswered questions but I have no choice and need to get this working.
I am hoping someone has done this in PHP and can provide me with an example or point me in the right path.
I was able to get the original code to work doing two things:
(1) Removing the leading space from $url:
# Original
$url = ' https://service.ringcentral.com/faxapi.asp';
# New
$url = 'https://service.ringcentral.com/faxapi.asp';
(2) Ensuring ROOT_PATH began with a # as specified in the PHP documentation for CURLOPT_POSTFIELDS at http://php.net/manual/en/function.curl-setopt.php.
cURL and Guzzle Examples
Here are some examples using cURL and Guzzle verified to work.
cURL Example
function ringcentral_faxout_api_via_curl($username,$password,$recipient,$file,$coverpagetext) {
$request = curl_init('https://service.ringcentral.com/faxapi.asp');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, array(
'username' => $username,
'password' => $password,
'recipient' => $recipient,
'attachment' => '#' . realpath($file),
'coverpagetext' => $coverpagetext
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($request);
curl_close($request);
return $response;
}
$username = 'myusername';
$password = 'mypassword';
$recipient = 'myrecipient';
$file = '/path/to/myfile';
$result = ringcentral_faxout_api_via_curl( $username, $password, $recipient, $file, 'PHP FaxOut Via cURL');
Guzzle Example
use GuzzleHttp\Client;
function ringcentral_faxout_api_via_guzzle($username,$password,$recipient,$file,$coverpagetext) {
$client = new Client();
$response = $client->post('https://service.ringcentral.com/faxapi.asp', [
'body' => [
'username' => $username,
'password' => $password,
'recipient' => $recipient,
'attachment' => fopen($file, 'r'),
'coverpagetext' => $coverpagetext
]
]);
return $response->getBody();
}
$username = 'myusername';
$password = 'mypassword';
$recipient = 'myrecipient';
$file = '/path/to/myfile';
$result = ringcentral_faxout_api_via_guzzle( $username, $password, $recipient, $file, 'PHP FaxOut Via Guzzle');
New RingCentral API
Also check out the newer RingCentral Platform API which has a much more comprehensive API for faxing and other capabilities documented here: https://developers.ringcentral.com/api-and-docs.html
function fetch_url_post($url, $variable_array){
$fields_string = "";
//set POST variables
#$url = 'http://domain.com/get-post.php';
foreach($variable_array as $key => $value){
$fields[$key] = urlencode($value);
}
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
return $result;
//close connection
curl_close($ch);
}
$url = ' https://service.ringcentral.com/faxapi.asp';
$faxData = array();
$faxData['Username'] = 'xxxxxxxx';
$faxData['Password'] = 'xxxxxxxx';
$faxData['Recipient'] = $faxNumber.'|TEST';
$faxData['Attachment'] = ROOT_PATH.$fileLocation;
echo fetch_url_post($url, $faxData);
make sure ROOT_PATH.$fileLocation; is an absolute and correct path
I currently have an API script that returns JSON. It has worked up until I tried to add in a curl php POST script before it. The curl script is working on it's own, and it is also working in the API script. However the JSON code is not being returned.
Is there something fundamentally wrong with this approach below?
Thanks in advance.
EDIT: The curl script works 100% on its own.
Said script is also working inside the below, it's just that the JSON does not return.
$name = "foo";
$age = "bar";
//set POST variables
$url = 'https://www.example.com';
$fields = array(
'name' => urlencode($name),
'age' => urlencode($age)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_encode(
array(
"status" => 1,
"message" => "Success!",
"request" => 10
)
);
You need to do the following use echo and also use CURLOPT_RETURNTRANSFER if not the output would be transferred directly to the page instead of $result
$name = "foo";
$age = "bar";
$url = 'http://.../a.php';
$fields = array('name' => urlencode($name),'age' => urlencode($age));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($ch);
curl_close($ch);
header('Content-type: application/json');
echo json_encode(array("status" => 1,"message" => "Success!","request" => 10));
this is a simple function to upload files remotely via php to rapidshare, requires username and password and curl activated in your server.
where is global, you can set the username and password anywhere outside the function
function upload_Rapidshare_remote($fileurl)
{
global $username, $password;
//Define the variables for post
$url = 'http://rapidshare.com/cgi-bin/rsapi.cgi';
$fields = array('sub' => "remotegets",
'cmd' => "addjob",
'login' => $username,
'password' => $password,
'urls' => urlencode($fileurl)
);
//Create url for post data
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//Open the Connection
$ch = curl_init($url);
//Send Post Data
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//Execute
$result = curl_exec($ch);
//Close the connection
curl_close($ch);
//return....
return;
}
Method of Use
upload_Rapidshare_remote('http://www.mysite.com/file.zip');
while($dataR = mysql_fetch_array($data)){
$postcode = str_replace(" ", "+", $dataR['Postcode']);
echo $postcode."<br />";
$oPostcode = $dataR['Postcode'];
// Retrieve the DOM from a given URL
$url = 'http://www.1.com';
$fields = array(
'txtPostCode'=>urlencode($oPostcode)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
curl_close($ch);
unset($ch);
//close connection
$html = str_get_html($result);
print_r($html);
}
Thats my code. However the cURL section only runs on the first time - what must I do? I have tried to understand the curl_multi_exec but can't find a simple answer.
It would be best to separate your cURL request from the loop...
so something like this would do..
while($dataR = mysql_fetch_array($data)){
$postcode = str_replace(" ", "+", $dataR['Postcode']);
echo $postcode."<br />";
$oPostcode = $dataR['Postcode'];
// Retrieve the DOM from a given URL
$url = 'http://www.1.com';
$fields = array(
'txtPostCode'=>urlencode($oPostcode)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
// Execute youru cURL here.
$data = array(
'url' => $url,
'fiels' => count($fields),
'field_string' => $fields_string
);
executecURL($data);
$html = str_get_html($result);
print_r($html);
}
function executecURL($data) {
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$data['url']);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,count($data['fields']));
curl_setopt($ch,CURLOPT_POSTFIELDS,$data['fields_string']);
//execute post
$result = curl_exec($ch);
curl_close($ch);
unset($ch);
//close connection
}
Try unset'ting the $field & $field_string
unset($fields);
unset($fields_string);
before re-using it