Please am creating a web application that sends sms alert when a user is registered. i wrote the script to do that which is below, i then uploaded that file to my web server for testing but it refuse to work. am actually having issues understanding curl. can someone tell me what am not doing right?
<?php
$number='08166848961';
$message_body='my test from server';
echo CURLsendsms($number,$message_body);
function CURLsendsms($number, $message_body){
$type='0';
$routing='3';
$token = 'VMnzTxbzgFKs5Po2vt6BhVt6VSWWNSDuaKAeI4Nch2cL4USf6furZ7ckVSc4Qf8jk';
$api_params ='message='.$message_body.'&to='.$number.'&sender='.$number.'&type='.$type.'&routing='.$routing.'&token='.$token;
$smsGatewayUrl = "https://smartsmssolutions.com/api/?";
$smsgatewaydata = $smsGatewayUrl.$api_params;
$url = $smsgatewaydata;
$ch = curl_init(); // initialize CURL
curl_setopt($ch, CURLOPT_POST, false); // Set CURL Post Data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch); // Close CURL
// Use file get contents when CURL is not installed on server.
if(!$output){
return $output = file_get_contents($smsgatewaydata);
}else
}
?>
Related
I'm working on a project and am attempting to link a Java application to my website/database via an API.
I'm running on an aws ec2 instance. The problem I am encountering is using Curl to POST data to another webpage. (The end-product will require proxies, and I've decided to use curl to manage this.)
The PHP code that makes the request:
<?php
function get_url($url)
{
$ch = curl_init();
if($ch === false)
{
die('Failed to create curl object');
}
// Temporary, just trying to get POST value to work.
$data = "password=temp";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data2 = curl_exec($ch);
curl_close($ch);
return $data2;
}
echo get_url("www.mywebsite.org/API/myscript.php");
?>
The server-side code that handles the request:
<?php
echo "Password: " . $_POST["password"];
?>
The issue I am having is with passing the post variables through curl and retrieving them.
The output on the page is:
Password:
and the POST variables are not being set. I'm thinking I am missing a CURLOPT somewhere but can't figure out which one!
I need to get Icecast metadata auto update by PHP let say every 15 min which will be done by cPanel cronjob.
i had the below code but it does't work(it works if I use header location to redirect however cronjob won't be able to do that)
<?PHP
$url="http://tgftp.nws.noaa.gov/data/observations/metar/stations/KJFK.TXT";
$info=file_get_contents($url);
$url_info = "http://username:password#icecast:8000/admin/metadata?mount=/mymount&mode=updinfo&song=" . urlencode($info);
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url_info);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Try checking for errors after you execute the call using curl_error:
<?php
$url="http://tgftp.nws.noaa.gov/data/observations/metar/stations/KJFK.TXT";
$info=file_get_contents($url);
$url_info = "http://username:password#icecast:8000/admin/metadata?mount=/mymount&mode=updinfo&song=" . urlencode($info);
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url_info);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser, check for errors
if (curl_exec($ch) === FALSE)
{
print 'Curl-Error occurred: ' . curl_error($ch).', error code: '.curl_errno($ch);
}
// close cURL resource, and free up system resources
curl_close($ch);
I'm new here so please be patient, This is my request code, I want to write another piece of code in another server that will be accepting it and returning an xml 7101 success code using php and curl. How do I write code in a receiving server. This is actually API that I will be using to exchange data between my system and another remote system stored in clouds I have managed to send request successfully but I have stuck at receiving their requests, I have spent an entire week on it please help me out on receiving the requests when they send them to me.
success code is
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gepgBillSubReqAck>
<TrxStsCode>7101</TrxStsCode>
</gepgBillSubReqAck>
the request code is this,
<?php
//Bill Request
$content ="<gepgBillSubReq>
<BillHdr>
<SpCode>SP103</SpCode>
<RtrRespFlg>true</RtrRespFlg>
</BillHdr>
<BillTrxInf>
<BillId>1812022009968</BillId>
<SubSpCode>1001</SubSpCode>
<SpSysId>TANESCO001</SpSysId>
<BillAmt>1180</BillAmt>
<MiscAmt>0</MiscAmt>
<BillExprDt>2017-12-31T23:59:59</BillExprDt>
<PyrId>5144AA5914</PyrId>
<PyrName>test</PyrName>
<BillDesc>Bill Number 5913</BillDesc>
<BillGenDt>2017-12-21T09:39:00</BillGenDt>
<BillGenBy>Bhstenkubo</BillGenBy>
<BillApprBy>Bhstenkubo</BillApprBy>
<PyrCellNum/>
<PyrEmail/>
<Ccy>TZS</Ccy>
<BillEqvAmt>1180</BillEqvAmt>
<RemFlag>false</RemFlag>
<BillPayOpt>1</BillPayOpt>
<BillItems>
<BillItem>
<BillItemRef>5144AA5914</BillItemRef>
<UseItemRefOnPay>N</UseItemRefOnPay>
<BillItemAmt>1180</BillItemAmt>
<BillItemEqvAmt>1180</BillItemEqvAmt>
<BillItemMiscAmt>0</BillItemMiscAmt>
<GfsCode>140316</GfsCode>
</BillItem>
</BillItems>
</BillTrxInf>
</gepgBillSubReq>";
$resultCurlPost = "";
$serverIp = "XXXXXXXXXXXXXXXX";
$uri = "/XXX/XXX/qrequest"; //this is for qrequest
echo "Message ready to GePG:"."\n".$content."\n";
$ch = curl_init($serverIp.$uri);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/xml',
'Gepg-Com:default.sp.in',
'Gepg-Code:SP103',
'Content-Length:'.strlen($content))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 50);
//Capture returned content from GePG
$resultCurlPost = curl_exec($ch);
curl_close($ch);
//$resultCurlPost=$data;
if(!empty($resultCurlPost)){
echo "\n\n";
echo "Received Response\n";
echo $resultCurlPost;
echo "\n";
}
else
{
echo "No result Returned"."\n";
}
?>
Thanks, you guys for contributing I found a solution, I used file_get_contents("php://input"); on the receiving server and so far it is working fine.
<?php
$message=$_POST["msg"];
echo $message;
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://.../pl1.php?
m=$message");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Above is the code iam using.anyone suggest me how do i get value of m in pl1.php
I am getting following error:
Your browser sent a request that this server could not understand.
In PHP variables passed as query string inside URL are accessible in $_GET array.
In your case it will be $_GET['m'].
Try this code:
<?php
$message=$_POST["msg"];
echo $message;
// curl initialize
$ch = curl_init();
// set curl options
curl_setopt($ch, CURLOPT_URL, "http://.../pl1.php?
m=$message");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute request and return response
$output = curl_exec($ch);
echo $output;
// close cURL request
curl_close($ch);
?>
And to get that data on page pl1.php use $_GET['m'];
But, I believe sending data using curl with post method is better than get method.
Thank you
I have a Flask app, with a basic function, where I have exposed app.run() to a public ip, so that it is accessible from an external server;[ using Flask - Externally Visible Dev Server ]
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 8080)
The curl request I have written in my php code is:
$signed_url = "http://my-ip-address:8080/";
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data= curl_exec($ch);
echo $data;
I can do a curl request :
curl http://my-ip-address:8080/
from command line. However, when the curl request is embedded within my PHP code, it gives me an error "Connection refused".
Kindly help!
If the PHP code is on another server, but your command line cURL request is on the same server, then you aren't comparing apples to apples.
Two things that might be wrong:
Your Flask server has a firewall that doesn't allow external connections.
You are connecting using an private network IP address rather than a public IP address.
For now your PHP code looks correct, so I would narrow down the problem a little bit. Ignore that PHP code and try to connect using cURL on the command line from the same server you are running your PHP code on.
try to set your port with curl options like this:
curl_setopt($ch, CURLOPT_PORT, 8080);
so your signed url will be:
$signed_url = "http://my-ip-address";
I use this code for my work and worked :)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:5000/spmi/api/1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"teks_analysis\":\"tidak ada skor nol\"}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
the key is CURLOPT_POSTFIELDS