Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using "sockets" or "scraping" in php

Posted on 2008-10-14
3
Medium Priority
?
461 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 2000 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:Richard Quadling
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
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…

688 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