post request in php

He has: 20 posts

Joined: Dec 2008

How can I make a post request in php without using a form!

I am trying to make an php parser for a site!

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Check out the Snoopy library. It is a library that helps you fetch pages, post data, and more. Post back if you want some code examples.

He has: 20 posts

Joined: Dec 2008

Hello!

Thanks for the library but I could not find any documentation about it!
It will be grate if you will give a code sample for the post request:
URL : http://www.example.com
postContent : param1=value1&param2=value2&param2=value2

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Here you go:

<?php
   
require('snoopy.php');
   
   
$snoopy = new Snoopy();
   
   
$url = 'http://www.example.com';
   
   
$data['param1'] = 'value1';
   
$data['param2'] = 'value2';
   
$data['param3'] = 'value3';
   
   
$snoopy->submit($url, $data);
?>

You can Google search "snoopy examples" and find some more helpful ways to request page, scan a page for links, troubleshoot errors, etc. Hope this helps. Smiling

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

This is most likely a class to wrap the curl function, probably with some more advanced features, but if you just need a down and dirty script

<?php

  $aryPost
= array('param1'=>"value1",
                  
'param2'=>"value2",
                  
'param3'=>"value3");
 
$postVals = '';
  foreach(
$aryPost as $key=>$val)
   
$postVals .= urlencode($key)."=".urlencode($val)."&";
            
 
$ch = curl_init("http://www.example.com");
 
curl_setopt($ch, CURLOPT_POST, 1); // Sending Post Items
 
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postVars,0,-1)); (chop of ending &
 
curl_setopt($ch, CURLOPT_HEADER, 0); // Don't include headers in result
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return page instead of display
 
$remotePage = curl_exec($ch);
 
curl_close($ch);

?>

PS. I have no idea why the $aryPost = array( line is wrapping like it does... shows fine in the editor... (grumble about this RTE)

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.