Solved

using "sockets" or "scraping" in php

Posted on 2008-10-14
3
430 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

743 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

11 Experts available now in Live!

Get 1:1 Help Now