URL Variable from text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hishamomran
Forum Newbie
Posts: 7
Joined: Tue Jan 04, 2011 10:25 am

URL Variable from text file

Post by hishamomran »

Ok, here's my problem! I have a text file which contains about 2000 cell phone numbers and an account on an online SMS gateway which gave me that link to send a message.

http://www.website.com/service.asmx?sender=000000&message=message&receiver=000000

Now I'm trying to create a php script to load the value of the $receiver either from a text file which would have the numbers separated by a line break or from a text box in a form that would also separate the numbers according to line breaks. Then I want the script to visit that URL while changing the value of the &receiver=VALUE every single time to one of the numbers from the text file or the form and when the success code is returned it moves on to the next number.

What I have so far:

Code: Select all

<?php
$FileName = "info.txt";
$FileHandle = fopen($FileName,"r");
$FileContent = fread ($FileHandle,filesize ($FileName));
fclose($FileHandle);
 
// You can replace the \t with whichever delimiting character you are using
$SplitContent = explode("\t", $FileContent);

$arrayVar=serialize(urlencode($SplitContent)); 


foreach($SplitContent as $CurrValue)
{


header('Location: http://www.website.com/service.asmx?SMSText=TEST&SMSSender=000000&SMSReceiver=' . $CurrValue);

}
?>
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: URL Variable from text file

Post by Neilos »

Use the file() function to create an array of the numbers;

Code: Select all

$numbers = file('file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$numbers = array_map('rtrim', $numbers);
Then use a foreach() to iterate through the array;

Code: Select all

foreach($numbers as $value) {
header('Location: http://www.website.com/service.asmx?SMSText=TEST&SMSSender=000000&SMSReceiver=' . $value);
}
Is how I'd do it. However...

How are you receiving a success code in order to move on to the next number? I guess that this is your real question, how to do this?

The nature of php is that it can only receive new input with each POST ie a page refresh. This is not a really efficient way of achieving this. If you want to do it with php then maybe send all the headers and recieve a list of successes and remove these ones from the list. Then rinse and repeat until you succeed in all or have a try limit = 5 or something.

Else if you want to send one header and wait for the success before moving on, php is not your best bet. AJAX would be a better choice.

I hope I have understood lol. Does this help?
hishamomran
Forum Newbie
Posts: 7
Joined: Tue Jan 04, 2011 10:25 am

Re: URL Variable from text file

Post by hishamomran »

I don't mind not getting the success code for now but the problem is that this solution redirects to http://website.com/service.asmx?sender= ... 0149965299

It does not separate the numbers, rather appends them all and run one parse for the URL.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: URL Variable from text file

Post by Neilos »

Whoops I think that we are missing something more elementary here lol.

You cannot keep sending headers in a foreach loop, you can only send headers once. Best you can hope to achieve is sending one header with the correct number.

I'll do a bit of research quick to see what I come up with.

However for now it'd be good to check that we are receiving all the numbers in the array properly;

Try this to see if you are getting all the correct numbers in the array;

Code: Select all

$numbers = file('info.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$numbers = array_map('rtrim', $numbers);

foreach($numbers as $value) {
echo $value . '<br />';
}
edit: *** actually I see that you are already getting all the correct values in an array, you way is fine for this. The problem is how to send multiple URL requests from a php script. I'll research this a bit as I don't know how to do it.
hishamomran
Forum Newbie
Posts: 7
Joined: Tue Jan 04, 2011 10:25 am

Re: URL Variable from text file

Post by hishamomran »

It returns the correct array indeed.
0149965299 0149965299 0149965299

I've researched a bit into Ajax but can't seem to find anything to do this. I'm looking as well for the php solution for this but I'm waiting for you :)
Thanks for taking the time to help btw.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: URL Variable from text file

Post by Neilos »

I think that this is a solution but I have no definite solutions yet.

http://php.net/manual/en/book.curl.php
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: URL Variable from text file

Post by Neilos »

Code: Select all

curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue','HeaderName2: HeaderValue2'));
I found this and I think it does what you want. You can research it to check it out lol! :D
hishamomran
Forum Newbie
Posts: 7
Joined: Tue Jan 04, 2011 10:25 am

Re: URL Variable from text file

Post by hishamomran »

I know this is getting tiring for you but I'm kinda confused about how to implement this in my code.

Should I replace the HeaderValue and HeaderValue2 with my URL? And replace the for each function all together?

curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue','HeaderName2: HeaderValue2'));
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: URL Variable from text file

Post by Neilos »

Code: Select all

$numbers = file('info.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$numbers = array_map('rtrim', $numbers);

foreach($numbers as $value) {

$url = 'http://www.website.com/service.asmx?SMSText=TEST&SMSSender=000000&SMSReceiver=' . $value;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $url);
curl_exec($ch );

}

curl_close($ch );
I threw this together, it is untested and I have no idea if it works. But I think it is the general idea.

To be honest I have no experience with cURL so this is a shot in the dark from me, I think that cURL can be used to sent lots of HTTP requests but I don't know.

Maybe someone with more experience using cURL can shed some light? :(

Lol let me know how you get on with this.
hishamomran
Forum Newbie
Posts: 7
Joined: Tue Jan 04, 2011 10:25 am

Re: URL Variable from text file

Post by hishamomran »

It gives me a 400 Bad Request error when I run the file locally and a 500 error when hosted on bluehost.com

Network Access Message: The page cannot be displayed
Explanation: There is a problem with the page you are trying to reach and it cannot be displayed.

Try the following:
Refresh page: Search for the page again by clicking the Refresh button. The timeout may have occurred due to Internet congestion.
Check spelling: Check that you typed the Web page address correctly. The address may have been mistyped.
Access from a link: If there is a link to the page you are looking for, try accessing the page from that link.
If you are still not able to view the requested page, try contacting your administrator or Helpdesk.

Technical Information (for support personnel)
Error Code: 400 Bad Request. The data is invalid. (13)
IP Address: website link
Date: 04/01/2011 06:38:42 م
Server: victorylink3
Source: proxy
hishamomran
Forum Newbie
Posts: 7
Joined: Tue Jan 04, 2011 10:25 am

Re: URL Variable from text file

Post by hishamomran »

Neilos, any updates? =]
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: URL Variable from text file

Post by Neilos »

Code: Select all


$numbers = file('info.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$numbers = array_map('rtrim', $numbers);

foreach($numbers as $SMSReciever) {

$SMSText = 'TEST';
$SMSSender = '000000';

$url = 'http://www.website.com/service.asmx';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,
	'SMSText=' . $SMSText . '&SMSSender=' . $SMSSender . '&SMSReciever=' . $SMSReciever);
curl_exec($ch );

}

curl_close($ch );

Try this.
hishamomran
Forum Newbie
Posts: 7
Joined: Tue Jan 04, 2011 10:25 am

Re: URL Variable from text file

Post by hishamomran »

Code: Select all

System.InvalidOperationException: Missing parameter: UserName. at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request) at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() System.InvalidOperationException: Missing parameter: UserName. at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request) at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() System.InvalidOperationException: Missing parameter: UserName. at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request) at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
Gives me that both on the server and hosted locally! =[
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: URL Variable from text file

Post by Neilos »

OK I have just tested the code and I can get it working, I made it loop through the foreach() loop 3 times each time making a http request (so that is 3 in total), posting some variables to a script which then takes those variables and adds them to a database.

I haven't yet managed to get the variables to actually post but I think that this is a simple problem. But I can however see that there are 3 separate records created in the database so I know that the cURL is running the script 3 times. I did this all by using a modified version of the code I gave you last (I only modified it to make it work with an example I had)

Code: Select all

<?php

$name[0] = $_POST["name1"];
$name[1] = $_POST["name2"];
$name[2] = $_POST["name3"];

$number[0] = $_POST["number1"];
$number[1] = $_POST["number2"];
$number[2] = $_POST["number3"];

$i = 0;

foreach($name as $value) {

$url = 'http://www.website.com/testinput.php';

$string["name"] = $value;
$string["number"] = $number[$i];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_exec($ch);

$i++;

}

curl_close($ch);

?>
Like I said, the post variables aren't actually being passed to testinput.php yet but it is a simple problem to solve I think but I am going to watch a film now so I have no time tonight. The principle is still the same so if you can't get it working you'll have to post your code.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: URL Variable from text file

Post by Neilos »

The problem with the POST variables was in testinput.php not in this code. This code works fine. Let me know if you don't get it working.
Post Reply