Jump to content

New to sockets


jmace

Recommended Posts

I am trying to post a feed file to ebay from our site via a PHP script, but I am new to this stuff. I found some instructions here: http://pics.ebaystatic.com/aw/pics/pdf/us/file_exchange/File_Exchange_Advanced_Instructions.pdf

 

The most important part of that PDF is:

Sample HTTP Post Request

POST /path/to/upload/script HTTP/1.0

Connection: Keep-Alive

User-Agent: My Client App v1.0

Host:

https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload

Content-type: multipart/form-data;

boundary=THIS_STRING_SEPARATES

Content-Length: 256

--THIS_STRING_SEPARATES

Content-Disposition: form-data; name="token"

12345678987654321

--THIS_STRING_SEPARATES

Content-Disposition: form-data; name="file";

filename="listings.csv"

Content-Type: text/csv

... contents of listings.csv ...

--THIS_STRING_SEPARATES

 

 

I tried to implement it using this code:

<?php

// GET THE FEED FILE
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => false, 
    CURLOPT_FOLLOWLOCATION => true, 
    CURLOPT_ENCODING       => "", 
    CURLOPT_USERAGENT      => "website", 
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 120, 
    CURLOPT_TIMEOUT        => 120,
    CURLOPT_MAXREDIRS      => 10, 
    CURLOPT_SSL_VERIFYPEER => false,
);

$ch = curl_init('https://www.mysite.com/pull_feed.php');
curl_setopt_array( $ch, $options );
$contents = curl_exec( $ch );
curl_close( $ch );


//----------------------------------------------------//


//OPEN THE CONNECTION
$conn = fsockopen('https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeProgrammaticDownload', 80);

// START THE REQUEST
fputs($conn, "POST ".$_SERVER['PATH_INFO']." HTTP/1.0");
fputs($conn, "Connection: Keep-Alive");
fputs($conn, "User-Agent: My Client App v1.0");
fputs($conn, "Host:");
fputs($conn, "https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload");
fputs($conn, "Content-type: multipart/form-data;");
fputs($conn, "boundary=THIS_STRING_SEPARATES");
fputs($conn, "Content-Length: ".strlen($contents));
fputs($conn, "--THIS_STRING_SEPARATES");
fputs($conn, "Content-Disposition: form-data; name=\"token\"");
fputs($conn, "MY_KEY_IS_HERE_000000000000000000000000");
fputs($conn, "--THIS_STRING_SEPARATES");
fputs($conn, "Content-Disposition: form-data; name=\"file\";");
fputs($conn, "filename=\"listings.csv\"");
fputs($conn, "Content-Type: text/csv");

// SEND THE FILE
fputs($conn, $contents);

// END THE REQUEST
fputs($conn, "--THIS_STRING_SEPARATES");

// GET THE RESULT
while(!feof($conn)) {
    echo fgets($conn, 128);
}

// CLOSE CONNECTION
fclose($conn);

?>

 

UPDATE: I accidently had error reporting off. Whoops. It is giving me this error:

Warning: fsockopen() [function.fsockopen]: unable to connect to https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeProgrammaticDownload:80 (Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?) in C:\home\imafs\public_html\funad\ebay\send_feed.php on line 27

Followed by more errors resulting from that one.

Link to comment
https://forums.phpfreaks.com/topic/219625-new-to-sockets/
Share on other sites

Also, you open sockets to servers, not URLs.

Also, each request header needs to be followed by a \r\n, with an extra one at the very end of them (ie, between the headers and the request body).

Also, you aren't doing the request body correctly. Learn about multipart/form-data.

Also, considering how incorrect that stuff in the PDF is, I'm suspicious of the Host: header.

 

Learn about HTTP and basic networking before trying to implement anything. Really. Stop working with PHP right now and go read Wikipedia or get a book on it or something.

Link to comment
https://forums.phpfreaks.com/topic/219625-new-to-sockets/#findComment-1138865
Share on other sites

@Thorpe: Thanks for your reply. If my server can't use https, what alternative is there? Or what should I do?

 

@Requinix: Thanks for the insights and the tips. Where do you suggest I go to learn about all of this? I am on a time crunch and need to get this done as quick as I can, so a suggestion on what tutorials are best would be very helpful.

 

Thank you both again.

Link to comment
https://forums.phpfreaks.com/topic/219625-new-to-sockets/#findComment-1138977
Share on other sites

Based on your comments and some more poking around on the internet, I changed code to this:

<?php

// GET THE FEED FILE
$options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle all encodings
    CURLOPT_USERAGENT      => "website", // who am i
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    CURLOPT_SSL_VERIFYPEER => false,
);

$ch = curl_init('https://www.mysite.com/pull_feed.php');
curl_setopt_array( $ch, $options );
$contents = curl_exec( $ch );
curl_close( $ch );


//----------------------------------------------------//


//OPEN THE CONNECTION
$conn = fsockopen('ssl://bulksell.ebay.com', 443);

// START THE REQUEST
fputs($conn, "POST ".$_SERVER['PHP_SELF']." HTTP/1.0\r\n");
fputs($conn, "Connection: Keep-Alive\r\n");
fputs($conn, "User-Agent: My Client App v1.0\r\n");
fputs($conn, "Host:\r\n");
fputs($conn, "https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload\r\n");
fputs($conn, "Content-type: multipart/form-data;\r\n");
fputs($conn, "boundary=THIS_STRING_SEPARATES\r\n");
fputs($conn, "Content-Length: ".strlen($contents)."\r\n");
fputs($conn, "--THIS_STRING_SEPARATES\r\n");
fputs($conn, "Content-Disposition: form-data; name=\"token\"\r\n");
fputs($conn, "MY_SECRET_TOKEN\r\n");
fputs($conn, "--THIS_STRING_SEPARATES\r\n");
fputs($conn, "Content-Disposition: form-data; name=\"file\";\r\n");
fputs($conn, "filename=\"listings.csv\"\r\n");
fputs($conn, "Content-Type: text/csv\r\n\r\n");

// SEND THE FILE
fputs($conn, $contents."\r\n");

// END THE REQUEST
fputs($conn, "--THIS_STRING_SEPARATES\r\n");

// GET THE RESULT
while(!feof($conn)) {
    echo fgets($conn, 128);
}

// CLOSE CONNECTION
fclose($conn);

?>

 

Which is working a lot better, but now the response I get back is this:

HTTP/1.1 302 Moved Temporarily

Server: Apache-Coyote/1.1

Location: http://pages.ebay.com/messages/page_not_found.html?eBayErrorEventName=p4apjlp%60jk%60jtb9%3Fjf.rdkhvjk357-2010.11.24.10.12.13.161.MST

Date: Wed, 24 Nov 2010 17:12:13 GMT

Connection: close

 

I am guessing it is because I am on the wrong page (ie bulksell.ebay.com instead of https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeProgrammaticDownload)

 

What should I do now?

Link to comment
https://forums.phpfreaks.com/topic/219625-new-to-sockets/#findComment-1139014
Share on other sites

  • 2 weeks later...

Well, I've made some progress, but still I still can't make it work.

 

Here is my code:

<?php

$key = "MY_KEY_HERE";

set_time_limit(0);

// GET THE FEED FILE
$options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle all encodings
    CURLOPT_USERAGENT      => "my app", // who am i
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    CURLOPT_SSL_VERIFYPEER => false,
);

$ch = curl_init('https://mysite.com/pull_feed.php');
curl_setopt_array( $ch, $options );
$contents = curl_exec( $ch );
curl_close( $ch );

//----------------------------------------------------//

// CREATE THE REQUEST
$request = "POST /ws/eBayISAPI.dll?FileExchangeUpload HTTP/1.1\r\n";
$request .= "Connection: Keep-Alive\r\n";
$request .= "User-Agent: My Client App v1.0\r\n";
$request .= "Host: bulksell.ebay.com\r\n";
$request .= "https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload\r\n";
$request .= "Content-type: multipart/form-data;\r\n";
$request .= "boundary=THIS_STRING_SEPARATES\r\n";
$request .= "Content-Length: ".strlen($contents)."\r\n";
$request .= "--THIS_STRING_SEPARATES\r\n";
$request .= "Content-Disposition: form-data; name=\"token\" \r\n";
$request .= $key."\r\n";
$request .= "--THIS_STRING_SEPARATES\r\n";
$request .= "Content-Disposition: form-data; name=\"file\";\r\n";
$request .= "filename=\"listings.csv\"\r\n";
$request .= "Content-Type: text/csv\r\n";
$request .= $contents."\r\n";
$request .= "--THIS_STRING_SEPARATES\r\n";

//OPEN THE CONNECTION
$conn = fsockopen('ssl://bulksell.ebay.com', 443);

// SEND THE REQUEST
fputs($conn, $request);

// GET THE RESULT
while(!feof($conn)) {
    echo fgets($conn, 128);
}

// CLOSE CONNECTION
fclose($conn);

?>

 

Here is the the request being sent:

POST /ws/eBayISAPI.dll?FileExchangeUpload HTTP/1.1
Connection: Keep-Alive
User-Agent: My Client App v1.0
Host: bulksell.ebay.com
https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload
Content-type: multipart/form-data;
boundary=THIS_STRING_SEPARATES
Content-Length: 4102
--THIS_STRING_SEPARATES
Content-Disposition: form-data; name="token" 
MY_KEY_HERE
--THIS_STRING_SEPARATES
Content-Disposition: form-data; name="file";
filename="listings.csv"
Content-Type: text/csv
. . . THE FILE CONTENTS ARE HERE . . . 

--THIS_STRING_SEPARATES

 

The script take a long time to run, and then it finally gives me the following response from ebay:

HTTP/1.1 200 OK

Server: Apache-Coyote/1.1

Set-Cookie: dp1=bu1p/QEBfX0BAX19AQA**4ed94120^; Domain=.ebay.com; Expires=Sat, 01-Dec-2012 21:20:32 GMT; Path=/

Set-Cookie: s=CgAD4ACBM+V8gYThmNTNiNGIxMmMwYTAyNjhlZTc0NGQ3ZmZmZjkxNjEechE8; Domain=.ebay.com; Path=/

Set-Cookie: nonsession=CgADKACBWXg8gYThmNTNiNGIxMmMwYTAyNjhlZTc0NGQ3ZmZmZjkxNjEAywABTPgUqDGEH+IQ; Domain=.ebay.com; Expires=Fri, 02-Dec-2011 21:20:32 GMT; Path=/

Cache-Control: private

Pragma: no-cache

Content-Type: text/html;charset=UTF-8

Content-Length: 755

Date: Thu, 02 Dec 2010 21:20:32 GMT

 

<html>

<head>

<!--eBay V3- msxml 6.0 XXXXXXXXXXXXXXXXXXXXXXXXXX-->

<!--srcId - File Exchange Programmatically Upload-->

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

<title>Upload File Programmatically</title><script language="JavaScript"><!--

var pageName = "File Exchange Upload";

 

//--></script><script language="javascript" src="http://include.ebaystatic.com/js/e695/us/legacy/globals_e6951us.js"> </script><script src="http://include.ebaystatic.com/js/e695/us/legacy/common_functions_e6951us.js"> </script></head>

<body>

An unknown problem interrupted the file transfer. Please try again.<br><a href="javascript:void(0);" onclick="self.close();return false;">Close</a></body>

</html>

 

<br />

<b>Warning</b>:  fgets() [<a href='function.fgets'>function.fgets</a>]: SSL: The operation completed successfully.

in <b>C:\home\imafs\public_html\funad\ebay\send_feed.php</b> on line <b>57</b><br />

 

ANY help would be appreciated. I'm really having a hard time with this one.

 

Thanks,

jmace

Link to comment
https://forums.phpfreaks.com/topic/219625-new-to-sockets/#findComment-1142379
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.