Solved

using "sockets" or "scraping" in php

Posted on 2008-10-14
3
436 Views
Last Modified: 2013-12-13
I have heard that it is possible to log into other sites using php on a server...i've seen it referred to as scraping or opening a socket, neither of which I am familiar with (not sure if they're the same thing)

to clarify, i want to automate certain processes that take waaay to long to do manually...
so, i want to:

1. open a connection with a website (let's say Paypal)
2. enter my username and password and "click" the submit button
3. go to the "send payments" page
4. enter all the required info for a payment (taken from my database)
5. submit the payment form
6. repeat from step 3 for the next row in my database

I want to do all of this using php (preferably set up as a CRON job)

is this possible?
can i see a sample script?
how are cookies/sessions handled?

0
Comment
Question by:sjamal_hsj
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
RQuadling earned 500 total points
ID: 22723338
There are many ways to achieve this.

Personally, with a site like PayPal, I would go down their developers API route. Far safer and supported.

For other sites, which don't require security, you can use functions like file_get_contents() to read a page.

If you need more advanced GETting and POSTing, then you can use cURL, sockets or stream_contexts (my preference).

With a stream_context, you setup things like protocol, cookies, etc. for your communication to site x. THen all comms (using normal file_get_contents() again) take with the request the cookies or POSTed data or even file uploads.

But, for sites like PayPal, use their API.
0
 

Author Comment

by:sjamal_hsj
ID: 22734448
so, with stream_context, can you show me a quick sample script of how you would set up the post variables and then submit them to a site?

as an example, lets say the site uses the form i've included as a code snippet

also, once i log in using stream_context, my file_get_contents() will then return the page for a logged in user?
<form name="login" action="login.php" method="post">

  <input type="text" name="username" id="user" /><br>

  <input type="password" name="pwd" id="pwd1" /><br>

  <input type="submit" name="submitBtn" value="Login" />

</form>

Open in new window

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 22738635
Something like this ...
<?php

$data = array ('username' => 'some_username', 'pwd' => 'some_pwd', 'submitBtn' => 'Login');

$data = http_build_query($data);
 

$context_options = array (

        'http' => array (

            'method' => 'POST',

            'header'=> "Content-type: application/x-www-form-urlencoded\r\n"

                . "Content-Length: " . strlen($data) . "\r\n",

            'content' => $data

            )

        );
 

$context = context_create_stream($context_options)

$HTML = file_get_contents('http://url', FILE_BINARY, $context);

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
File submission to SQL Server 1 37
Download a website to hdd 2 47
form button worked now it doesnt anymore 9 36
How can I echo a PHP variable in AJAX function? 7 27
This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now