Make a PHP GET request from a PHP script and exit - php

Is there something simpler than the following.
I am trying to make a GET request to a PHP script and then exit the current script.
I think this is a job for CURL but is there something simpler as I don't want to really worry about enabling the CURL php extension?
In addition, will the below start the PHP script and then just come back and not wait for it to finish?
//set GET variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname'=>urlencode($last_name),
'fname'=>urlencode($first_name)
);
//url-ify the data for the GET
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_GET,count($fields));
curl_setopt($ch,CURLOPT_GETFIELDS,$fields_string);
//execute GET
$result = curl_exec($ch);
//close connection
curl_close($ch);
I want to run the other script which contains functions when a condition is met so a simple include won't work as the if condition wraps around the functions, right?
Please note, I am on windows machine and the code I am writing will only be used on a Windows OS.
Thanks all for any help and advice

$url = 'http://domain.com/get-post.php?lname=' . urlencode($last_name) . '&fname=' . urlencode($first_name);
$html = file_get_contents($url);
If you want to use the query string assembly method (from the code you posted):
//set GET variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname'=>urlencode($last_name),
'fname'=>urlencode($first_name)
);
//url-ify the data for the GET
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$html = file_get_contents($url . '?' . $fields_string);
See:
http://php.net/manual/en/function.file-get-contents.php

Related

salesforce api add leads from form

I am trying to add leads from my contact form to salesforce. Currently I am using this solution that passes them through what I assume is the Web-to-Lead solution they have:
if (isset($_POST['submit'])) {
$ch_register_first_name = $_POST['ch_register_first_name'];
$ch_register_last_name = $_POST['ch_register_last_name'];
$ch_register_dob = $_POST['ch_register_dob'];
$ch_register_phone = $_POST['ch_register_phone'];
$ch_register_email = $_POST['ch_register_email'];
$ch_register_street = $_POST['ch_register_street'];
$ch_register_street2 = $_POST['ch_register_street2'];
$ch_register_city = $_POST['ch_register_city'];
$ch_register_state = $_POST['ch_register_state'];
$ch_register_zip = $_POST['ch_register_zip'];
//set POST variables
$url = 'https://test.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
$fields = array(
'last_name'=>urlencode($ch_register_first_name),
'first_name'=>urlencode($ch_register_last_name),
'street'=>urlencode($ch_register_street),
'city'=>urlencode($ch_register_city),
'state'=>urlencode($ch_register_state),
'zip'=>urlencode($ch_register_zip),
'company'=>urlencode($company),
'description'=>urlencode($ch_register_dob),
'email'=>urlencode($ch_register_phone),
'phone'=>urlencode($ch_register_email),
'mycustomefieldid' => urlencode($ch_register_dob), // custom field
'oid' => $ch_register_salesforce_id, // insert with your id
'retURL' => urlencode('http://thank-you/'), // sending this just in case
'debug' => '1',
'debugEmail' => urlencode("email#email.com"), // your debugging email
);
//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);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
}
The issue by doing this (if I am correct that its using the Web-to-Lead solution) the max limit is 500 in 24hrs. Some events this form will be used at day of will have more than 500 for sure. How can I use just their API I have read about? Or should I save the locally to the database then run a batch using the Force api?

URL truncated after ampersand when passed thru curl in PHP (linux) using POST method

PHP code:
$fields_string = "";
foreach ($postingfields as $key=>$value) {
$fields_string .= $key . '=' . urlencode($value) . '&';
}
$fields_string = rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$client_url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$response = curl_getinfo( $ch );
curl_close($ch);
$client_url php variable holds the value: https://pcloudtest.com/Default.aspx?cid=99938
$fields_string php variable holds the value: &sid=30&title=Mr&firstname=Charles&surname=Smith
The destination server has been set up to respond with the following HTML:
When I debug (send info to a separate txt file in linux) the value of $result is:
<URL>https://pcloudtest.com/Default.aspx?cid=99938</URL>
ie this is what the destination server is claiming has been sent to them from my end.
In other words, the $client_url is all that is being posted, and not the rest of it (ie the $fields_string) and the full URL that should've been posted should read:
https://pcloudtest.com/Default.aspx?cid=99938&sid=30&title=Mr&firstname=Charles&surname=Smith
I have tried everything I can to figure out why the php curl functions are apparently sending out a shortened URL, ie up to the first occurrence of an ampersand. The code logic I have above has not changed in months and is working for other destination servers.
I might add that the other destination servers where this logic has no issues are http: sites not https:. But I have been reassured by the tech guys on the other end that it definitely has nothing to do with posting to a https site.
Please help. I hope I have outlined my issue clearly enough, and if not, please advise as to more info I can provide.

send bulk sms from database

I've been trying to use the code below to send sms but it does not send when I loop. It only works if I just pick one number from database. I have over 5,000 numbers in the database and wish to send an sms to all of them at the same time, Please help.
mysql_select_db($database_xxx, $xxx);
$query_rs = "SELECT phone FROM `notify` order by id asc LIMIT $l1 , $l2";
$rs= mysql_query($query_rs, $xxx) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs= mysql_num_rows($rs);
$phone = $row_rs['phone'];
// Do while loop to send sms.
while($row=mysql_fetch_assoc($rs)){
// Let's do some formatting and keep smiling.
$giringirin = ereg_replace("[^0-9]", "", $phone );
if (strlen($giringirin) == 11) {
$phone1=substr($giringirin, 1);
$phone= "234$phone1";
} elseif (strlen($giringirin) == 13){
$phone = $giringirin;
}
extract($_POST);
//set POST variables
$url = "http://sms.xxx.com/bulksms/bulksms.php?username=$username&password=$password&message=$smsmessage&mobile=$phone&sender=$sender";
$fields = array(
);
//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);
if ($result == '1801') { echo "SMS has also been sent to the Customer ($phone) \n";} else { echo "Oooops, No sms was sent";}
//close connection
curl_close($ch);
}
Your code is confusing...
curl_setopt($ch,CURLOPT_POST,count($fields));
CURLOPT_POST is a boolean flag. Either you're doing a post, or you're not. The number of fields you're posting is irrelevant.
You're building up a series of variables/values to be posted, but doing it via string operations. CURL is perfectly capable of taking an array and doing all that for you, reducing your entire foreach loop to just
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
You're using extract() on $_POST, which pollutes your script's variable namespace with any garbage a malicious user cares to send over - you're essentially replicating PHP's utterly moronically brain-dead register_globals all over again.
You're using ereg, which has been deprecated for approximiately 5 million internet years. You should be using the preg functions insteadl
What happens when you run this script in the browser? Blank page? Something? Error?
Start by changing
$url = "http://sms.xxx.com/bulksms/bulksms.php?username=$username&password=$password&message=$smsmessage&mobile=$phone&sender=$sender";
to
$url = "http://sms.xxx.com/bulksms/bulksms.php?username=".$username."&password=".$password."&message=".$smsmessage."&mobile=".$phone."&sender=".$sender."";

PHP 5.3, Ampersand in URL passing not working

I have code that works fine for anything before 5.3, and now this code won't work in php 5.3:
foreach($_GET as $key => $value) {
$params .= "&";
$params .= $key."=".$value;
}
$fields = array(
'mode'=>urlencode('native'),
'cookie_file'=>urlencode($data['cookie_file']),
'full_status'=>urlencode('1'),
'remove_cookies'=>urlencode('1'),
'url'=>urlencode("http://www.domainblocked.com/blocked_name.php?" . $params)
);
//url-ify the data for the POST
$fields_string = "";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//open connection
$ch = curl_init();
$get_url = $proxy."?".$fields_string;
print $get_url;
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$get_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$html = curl_exec($ch);
The file $proxy refers to a PHP proxy which complains it never gets the $_GET['url'] variable despite the $get_url being:
http://www.domainblocked.com/proxy.php?mode=native&cookie_file=%2Ftmp%2FCURLCOOKIEQ1N7ki&full_status=1&remove_cookies=1&url=http%3A%2F%2Fwww.domainblocked.com%2Fblocked_name.php%3F%26room%3D3043542%3B3332215%3B4875165%3B6917670%3B7055833%3B7454057%3B7747396%3B8321764%3B8331103%3B8362498%3B8406246%3B8406305%3B8406671%3B8406728%3B8406965%3B9213754%3B9895515%3B9898865%3B9904319%3B9904558%3B9908327%3B9910403%3B9910760%3B9916830%3B9918045%3B9919351%3B9919603%26avatar69323038%3D%26avatar1%3D6268520%3B6713654%3B7347874%3B7738193%3B7780315%3B8493140%3B8501371%3B8752968%3B9487253%3B9524370%3B9811618%3B10196605%3B10519485%26avatar69527829%3D%26avatar2%3D223055%3B2690693%3B4228334%3B6251471%3B6270797%3B6713654%3B7122151%3B7198849%3B7644202%3B7868916%3B8501371%3B8698810%3B8739013%3B8739767%3B9142768%26avatar38051172%3D%26avatar3%3D4792397%3B6285528%3B7204362%3B7518963%3B7773830%3B7776340%3B8403813%3B8501371%3B9481162%3B9520771%3B9751483%3B9971760%3B9997875%3B10109398%3B10117824%26avatar227598%3D%26avatar4%3D1962690%3B4223019%3B5707484%3B6127072%3B7162053%3B7172244%3B7988191%3B8239420
therefore $_GET['url'] on the proxy file should be http%3A%2F%2Fwww.domainblocked.com%2Fblocked_name.php%3F%26room%3D3043542%3B3332215%3B4875165%3B6917670%3B7055833%3B7454057%3B7747396%3B8321764%3B8331103%3B8362498%3B8406246%3B8406305%3B8406671%3B8406728%3B8406965%3B9213754%3B9895515%3B9898865%3B9904319%3B9904558%3B9908327%3B9910403%3B9910760%3B9916830%3B9918045%3B9919351%3B9919603%26avatar69323038%3D%26avatar1%3D6268520%3B6713654%3B7347874%3B7738193%3B7780315%3B8493140%3B8501371%3B8752968%3B9487253%3B9524370%3B9811618%3B10196605%3B10519485%26avatar69527829%3D%26avatar2%3D223055%3B2690693%3B4228334%3B6251471%3B6270797%3B6713654%3B7122151%3B7198849%3B7644202%3B7868916%3B8501371%3B8698810%3B8739013%3B8739767%3B9142768%26avatar38051172%3D%26avatar3%3D4792397%3B6285528%3B7204362%3B7518963%3B7773830%3B7776340%3B8403813%3B8501371%3B9481162%3B9520771%3B9751483%3B9971760%3B9997875%3B10109398%3B10117824%26avatar227598%3D%26avatar4%3D1962690%3B4223019%3B5707484%3B6127072%3B7162053%3B7172244%3B7988191%3B8239420
But it never gets it.
If I dont urlencode the $_GET['url'] then it this:
http://www.domainblocked.com/proxy.php?mode=native&cookie_file=%2Ftmp%2FCURLCOOKIEjtmYSU&full_status=1&remove_cookies=1&url=http://www.domainblocked.com/blocked_name.php?&room=3043542;3332215;4875165;6917670;7055833;7454057;7747396;8321764;8331103;8362498;8406246;8406305;8406671;8406728;8406965;9213754;9895515;9898865;9904319;9904558;9908327;9910403;9910760;9916830;9918045;9919351;9919603&avatar69323038=&avatar1=6268520;6713654;7347874;7738193;7780315;8493140;8501371;8752968;9487253;9524370;9811618;10196605;10519485&avatar69527829=&avatar2=223055;2690693;4228334;6251471;6270797;6713654;7122151;7198849;7644202;7868916;8501371;8698810;8739013;8739767;9142768&avatar38051172=&avatar3=4792397;6285528;7204362;7518963;7773830;7776340;8403813;8501371;9481162;9520771;9751483;9971760;9997875;10109398;10117824&avatar227598=&avatar4=1962690;4223019;5707484;6127072;7162053;7172244;7988191;8239420
Which the proxy receieve $_GET['url'] but it's only http://www.domainblocked.com/blocked_name.php?
So I think it has to do with something with the & or urlencode in 5.3
foreach($_GET as $key => $value) {
$params .= "&";
$params .= $key."=".$value;
}
Switch this (and similar code) with http_build_query(). It will handle all the encoding.

json_decode in PHP to set a cookie

I want to decode Json returned from the WebService, And it should set a cookie, that i want to use to call next WebService API. I am not sure how to set a cookie, and decode this json.
I tried decoding it, but get the error. I need to extract sessionId. You can use the WebService.. I have put this on Internet.
Here is my code sample
<?php //extract data from the post
extract($_POST); //set POST variables
$url = 'http://202.83.243.119/ems/loginByEID.json';
$fields = array(
'eid'=>urlencode("7ea888b6-36e9-49db-84f3-856043841bef")
);
//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);
//decoding Json $obj =
json_decode($result);
print $obj->{'stat'};
?>
Use setcookie() to set a cookie.
Avoid extract() like the plague; it can be used to introduce any client-specified variables into your code.
Use $fields_string = http_build_query($_GET) to build a query string instead of your hodge-podge above.
Format your code properly. For example, you put the $obj = inside the previous comment line.

Categories