Jump to content

sending a multi-part POST using fsockopen


awi

Recommended Posts

Thanks in advance for attempting to help!

 

I have a php processor script that loops through an xml doc, extracts video data (names, file names...etc) and does a bunch of other stuff. Within this loop I need to have the script perform a "POST" basically emulating an HTML form but ofcourse in this case I do not want any HTML. I understand there are (mainly) 2 options to do this: fsockopen and curl. I would like to use fsockopen if possible, so I can get an http response code and hopefully analyze it when applicable.

 

I have tried to do this using fsockopen for a little over a day now with no success. I've read various posts and tutorials, but with no success so far. I know I could be missing something simple or maybe complex, I do not know any more, I'm going crazy with this  ;D

 

Can you please give me some guidance on how to send a POST using fsockopen for this situation:

 

lets say I want to send 3 pieces of data: clipTitle='Funny Clip', clipDescr='this is some descr', clipCategory='humor'

and I want to send (as if i'm uploading a file) file='1234.mpg' this file resides in the same dir as the script running this fsockopen.

 

I followed an example such as this one (modified for my situation) but no success:

http://www.thescripts.com/forum/thread588770.html

 

Thanks again!

CA

 

  • 2 years later...

I have never really used fsocks myself, but I have used PHP Curl, and it can return Apache codes. One of the curl settings displays the headers of the connection to Apache, as such, here is a script and its output run from my laptop (Xampp is Apache, PHP, etc)

 

HTTP/1.1 301 Moved Permanently Date: Sat, 22 May 2010 21:24:06 GMT Server: Apache/2.2.15 (Unix) mod_ssl/2.2.15 OpenSSL/0.9.8n DAV/2 mod_fcgid/2.3.5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 X-Powered-By: PHP/5.2.13 X-Pingback: http://iluvjohn.com/xmlrpc.php Location: http://iluvjohn.com/ Content-Length: 0 Content-Type: text/html; charset=UTF-8

 

So as you can see, after the HTTP/1.1 you have 301 Move Permanently. I am sure you can get the rest from there. The code below was tested and produced the header above.

 

Goodluck

 

~John

http://iluvjohn.com

 

<?php

 

$url = "http://www.iluvjohn.com/"; // the url we want to grab

$cookie_file = "cookie.txt"; // specify writable cookie txt file for cookies to be stored

//$post = "username=john&password=pass"; // post info, leave blank if we dont need it

$refer = "http://www.google.com/search?hl=en&q=fsockopen%20tcp"; // refering url

 

if(is_writable($cookie_file)){

$handle = fopen($cookie_file, 'w');

fclose($handle);

}else{

die("<div class=\"error\">Can't write to $cookie_file cookie file.</div>");

}

 

$ch = curl_init(); // start curl handler

curl_setopt($ch, CURLOPT_URL, $url); // what url you are going to access

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // I have never used this.

if(!empty($post)){curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);} // Send post info, like an html form

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt($ch, CURLOPT_HEADER, 1); // Use this to specify if you want headers returned or not. Set to 0 or 1

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); // user agent

    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); // Cookies

    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); // Cookies

    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); // Cookies

if(!empty($refer)){curl_setopt($ch, CURLOPT_REFERER, $refer);} // refer is the url you are coming from

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return the page content we accessed

$result = curl_exec($ch); // execute the curl settings we set, this is where the actual curl process happens.

curl_close ($ch); // close the curl handler

echo $result;

 

?>

 

 

[attachment deleted by admin]

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.