seeking adivce on php - cURL - And adding a variable

Discussion in 'PHP' started by jmort732, Apr 11, 2007.

  1. #1
    Hi All,
    Any advice or pointing me in the right direction would be greatly appreciated.

    Here is what I am trying to do:
    I want to add $variable to the end of a URL query string and then send it via post method and then retreive the answer.

    So here is what I have:
    
    $curl = curl_init( );
        curl_setopt($curl, CURLOPT_URL,"http://www.domain/page.asp?");
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, "a=123&b=345&c=xyz&d={$variable}");
    
        $result = curl_exec ($curl);
        curl_close ($curl);
    
    Code (markup):
    But its not working, for the life of me I can't figure it out. I have tried everything. Any advice? Am I missing something obvious?

    Thanks,
    Morty
     
    jmort732, Apr 11, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    The - is not a valid character for variables. Rename it to $a_variable or $avariable.
     
    nico_swd, Apr 11, 2007 IP
  3. jmort732

    jmort732 Peon

    Messages:
    543
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Woops, that is just an example, the real variable is all alpha chars
    I edited the original post, to correct.

    Any ideas one why curl won't post the query string?

    Thanks!
    Morty
     
    jmort732, Apr 11, 2007 IP
  4. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #4
    If you need to have your data in the URL, it is GET, not POST method. So your example can be:
    
    $curl = curl_init( );
    $query="a=123&b=345&c=xyz&d={$variable}";
    curl_setopt($curl, CURLOPT_URL,"http://www.domain/page.asp?$query");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec ($curl);
    curl_close ($curl);
    
    PHP:
    If I am wrong and you need POST, check that the URL http://www.domain/page.asp doesn't send any redirect as answer; if redirect is there, your data will not be posted. You can try curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true) in such case.
     
    wmtips, Apr 11, 2007 IP
  5. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #5
    Also you can get more information about last cURL call with
    echo '<pre>',print_r(curl_getinfo($curl)),'</pre>';
    PHP:
    and curl_error and curl_errno functions.
     
    wmtips, Apr 11, 2007 IP
  6. jmort732

    jmort732 Peon

    Messages:
    543
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #6
    wmtips, you the man!

    The issue was that the URL was being redirected, so the CURLOPT_FOLLOWLOCATION worked. Using the get method everything is working fine now. I was using the POST method, after unsuccessfully using the GET method and reading somewhere that the POST method should work for query strings.

    So to close up the thread and to help anyone in the future, here is the sample code that works:
    
    $curl = curl_init( );
    $query="a=123&b=345&c=xyz&d={$variable}";
    curl_setopt($curl, CURLOPT_URL,"http://www.domain/page.asp?$query");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec ($curl);
    curl_close ($curl);
    
    Code (markup):
    Thanks again!
    Morty
     
    jmort732, Apr 11, 2007 IP