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
Related
I have a php file (sample1.php) which calls another php file (sample2.php)
through header function.
header("Location: "sample2.php");
How can I pass a JSON object from one php to another.
I need to POST the JSON object, so that the second file can access it using
if( isset($_POST["json"]) ) { ... }
You can use cURL method
<?php
//set POST variables
$url = 'sample2.php';
$fields = array(
'json' => urlencode($json/*your json that want to pass*/),
);
//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);
?>
Or you can refer this link :)
If you absolutely have to use header() to proceed to sample2.php then you can't use $_POST to get the data when in sample2.php.
Put it in session storage (in sample1.php):
$_SESSION['jsonData'] = $yourJsonData;
Then after arriving in sample2.php:
if( isset($_SESSION["jsonData"]) ) { ... }
Don't forget to initialize your sessions, etc. Refer to http://php.net/manual/en/session.examples.basic.php
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
}
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));
Heres my bit o' php, tryin to figure out why I'm not getting a $result going to the URL it produces will give me a valid JSON result based on a get or post. So I think my problem is how I am using cURL. So I need someone take on this.
//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($params));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
if(!$result)
{
$error = curl_error($ch); echo $error; return false;
}
//close connection
curl_close($ch);
//return json_decode($result);
echo $result;
EDITED CODE ABOVE From original Post
$error does not report anything. I changed the return json... to echo to see if that was doing anything and it printed out 'Disallowed Key Characters.' on the screen.
EDIT 2
$url = http://domain.com/search
$params = array('q'=>'search,term')
$params is put through a foreach loop which builds the $fields_string
$fields_string = '?';
foreach($params as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = substr($fields_string,0,-1);
fields_string looks like ?ll=37.2790669,-121.874722&range=10 in the end (for what I am doing currently, I have anywhere from 1-12 optional parameters that can be passed which is why I am building it the way I am.
Try adding error checking to make sure curl_exec is executing successfully and to get a meaningful error message back on the server.
something like:
$ch = curl_init();
//Make string url safe with urlencode
curl_setopt($ch,CURLOPT_URL,urlencode($url));
curl_setopt($ch,CURLOPT_POST,count($params));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(!$result) {
$error = curl_error();
//$error now contains the error thrown when curl_exec failed to execute
//echo this to terminal or to an error box in the browser?
}
curl_close($ch);
return json_decode($result);
Post your resulting error here if you still need help.
Also, here are the manual pages for the two functions I took advantage of:
http://www.php.net/manual/en/function.curl-error.php
http://www.php.net/manual/en/function.curl-exec.php
http://php.net/manual/en/function.urlencode.php
-Cheers
//set POST variables
$url = "http://example.com/edsym/registration.php"; // URL to calc.cgi
$fields = array(
'namee'=>urlencode($name),
'city'=>urlencode($city),
'phh'=>urlencode($ph),
'emaill'=>urlencode($email),
'msg1'=>urlencode("Message type")
);
$fields_string=" ";
//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);
I am using curl to post like this and its working
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