cURL Example !

Discussion in 'PHP' started by priyakochin, Jun 11, 2007.

  1. #1
    Can anyone please give me some example script of cURL for multiple post and
    get ?
     
    priyakochin, Jun 11, 2007 IP
  2. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #2
    Zelune, a proxy site script, requires cURL for example.
     
    EGS, Jun 11, 2007 IP
  3. priyakochin

    priyakochin Banned

    Messages:
    4,740
    Likes Received:
    138
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am a newbie so will I get some small scripts ?
     
    priyakochin, Jun 11, 2007 IP
  4. Raithe

    Raithe Well-Known Member

    Messages:
    1,923
    Likes Received:
    177
    Best Answers:
    0
    Trophy Points:
    175
    #4
    Raithe, Jun 11, 2007 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    
    <?php
    
    class curl
    {
    	function curl( )
    	{
    		if( !( $this->curl = curl_init( ) ) )
    		{
    			die("cURL is not available on this server");	
    		}
    		elseif( !curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, true ) )
    		{
    			die("cURL setup failed, please insure curl is installed properly");	
    		}
    	}
    	function get( $url )
    	{
    		if( !curl_setopt( $this->curl, CURLOPT_URL, $url ) )
    		{
    			return false;
    		}
    		return curl_exec( $this->curl );
    	}
    	function post( $url, $params )
    	{
    		if( !curl_setopt( $this->curl, CURLOPT_POST, true ) or
    			!curl_setopt( $this->curl, CURLOPT_POSTFIELDS, $params ) or
    			!curl_setopt( $this->curl, CURLOPT_URL, $url ) )
    		{
    			return false;
    		}		
    		return curl_exec( $this->curl );
    	}
    	function request( $method, $url, $params = null )
    	{
    		if( strtolower( $method ) == 'post' ) return $this->post( $url, $params );
    		else return $this->get( $url );
    	}
    	function multi( $jobs, &$data )
    	{
    		foreach( $jobs as $count => $job )
    		{
    			if( !( $data[ $count ] = $this->request( $job[0], $job[1], $job[2] ) ) )
    			{
    				die( sprintf( "cURL failed with error code %s : %s", curl_errno( $this->curl ), curl_error( $this->curl ) ) );	
    			}
    		}				
    	}
    }
    $curl = new curl( );
    
    $curl->multi(
    	array(
    		array(
    			'POST',
    			'http://krakjoe.com/post-ajax.php',
    			'the=data&goes=here'
    		),
    		array(
    			'GET',
    			'http://msn.com',
    			null
    		)
    	),
    	$storage
    );
    
    // $storage now contains the data from the request above
    // print_r( $storage );
    ?>
    
    PHP:
     
    krakjoe, Jun 11, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    i would recommend using http_build_query() (if using php5) to build the query string. just how i would do it so you don't have to worry about url-encoding the args it's already done.

    
    $args = array("var1"=>"value1","var2"=>"value2","var3"=>"value3");
    $query_string = http_build_query($args);
    
    PHP:
     
    ansi, Jun 11, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    and if not ....

    
    <?php
    $args = array("var1"=>"value1 and this next bit","var2"=>"value2 and some more url encoded stuff","var3"=>"value3 and yet more encoded stuff");
    $query_string = http_build_query($args );
    
    if( !function_exists( 'http_build_query' ) )
    {
    	function http_build_query( $array, $numeric = null )
    	{
    		foreach( $array as $key => $value )
    		{
    			$result[] = sprintf( '%s=%s', $key, urlencode( $value ) );
    		}
    		return implode( $numeric ? $numeric : "&", $result );
    	}
    }
    
    echo $query_string ;
    
    PHP:
     
    krakjoe, Jun 12, 2007 IP