Solved

using "sockets" or "scraping" in php

Posted on 2008-10-14
3
444 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:
Richard Quadling 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: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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fulfillment API php code sample 1 58
Cookie not unsetting 7 27
Email called spam 5 36
Why is PHP Storm giving me an undefined constant error? 4 19
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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