SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick CURL question

    Just a quick question while I consider writing this... will the following cURL work?

    PHP Code:
    curl_setopt($chCURLOPT_POSTFIELDS"$sidVAR={$sid[$i2]}&$cvar={$cid[$i2]}&$sVAR={$aVAR}"); 

    Obviously missing code, just that CURLOPT_POSTFIELDS has me wondering if I can use variables like that.

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    It will work, sure. However, why do you want to construct a string like this anyway, sometimes using curly braces, sometimes not and not caring about proper url-encoded values?
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Cannot remember exactly but you could choke it on the "&" s, if it happens then expand them to &

    Get some reliable debug and error reporting going on otherwise cURL turns into a voodoo-ish blackbox in no time.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Just pass an associtive array for the post fields and be done with it, urlencoding and query-string building for free!

    PHP Code:
    <?php
    curl_setopt
    ($chCURLOPT_POSTFIELDS, array($sidVAR => $sid[$i2], $cvar => $cid[$i2], $sVAR => $aVAR));
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    Note that using an array for CURLOPT_POSTFIELDS will send the data as multipart/form-data which may or may not be a problem for your script. If you need it to be the more usual application/x-www-form-urlencoded then you can use:
    PHP Code:
    curl_setopt($chCURLOPT_POSTFIELDShttp_build_query(array(...))); 
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •