Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with PHP cURL request

    Hi guys,

    I was wondering if you could help me. I have this code which should send a call to a RESTful web service (which creates a new user in a database). My code is executing without any errors (that I can see) however the user doesn't get created.

    PHP Code:
    $url 'http://127.0.0.1:85/AccountCreator.ashx';
        
    $curl_post_data = array(
        
    '$companyName' =>$companyName,
        
    '$mainContact' =>$mainContact,
        
    '$telephone1' =>$telephone1,
        
    '$email' => $email,
        
    '$contact2' => $contact2,
        
    '$telephone2' => $telephone2,
        
    '$email2' => $email2,
        
    '$package' => $package
        
    );
        
    foreach(
    $curl_post_data as $key=>$value) {$fields_string.=$key'=' .$value.'&';
    }
    rtrim($fields_string'&');
    //die("Test: ".$fields_string);

    $ch curl_init();

    curl_setopt ($chCURLOPT$url);
    curl_setopt ($chCURLOPT_POSTtrue);
    curl_setopt ($chCURLOPT_POSTFIELDS$fields_string);

    $result curl_exec($ch);

    curl_close($ch); 
    I'm fairly new to PHP also, so I just can't see where I'm doing wrong. I have access to the server also - is there anything I can check on that which would indicate if the web service is being called or not?

    I've also read this in http://php.net/manual/en/function.curl-setopt.php...

    CURLOPT_POSTFIELDS: The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.
    I don't think my PHP document is set to multipart/form-data - Would this affect it?
    Last edited by t-bag; 09-28-2010 at 09:54 AM. Reason: additional info

  • #2
    New Coder
    Join Date
    Sep 2010
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by t-bag View Post
    Hi guys,

    I was wondering if you could help me. I have this code which should send a call to a RESTful web service (which creates a new user in a database). My code is executing without any errors (that I can see) however the user doesn't get created.

    PHP Code:
    $url 'http://127.0.0.1:85/AccountCreator.ashx';
        
    $curl_post_data = array(
        
    '$companyName' =>$companyName,
        
    '$mainContact' =>$mainContact,
        
    '$telephone1' =>$telephone1,
        
    '$email' => $email,
        
    '$contact2' => $contact2,
        
    '$telephone2' => $telephone2,
        
    '$email2' => $email2,
        
    '$package' => $package
        
    );
        
    foreach(
    $curl_post_data as $key=>$value) {$fields_string.=$key'=' .$value.'&';
    }
    rtrim($fields_string'&');
    //die("Test: ".$fields_string);

    $ch curl_init();

    curl_setopt ($chCURLOPT$url);
    curl_setopt ($chCURLOPT_POSTtrue);
    curl_setopt ($chCURLOPT_POSTFIELDS$fields_string);

    $result curl_exec($ch);

    curl_close($ch); 
    I'm fairly new to PHP also, so I just can't see where I'm doing wrong. I have access to the server also - is there anything I can check on that which would indicate if the web service is being called or not?

    I've also read this in http://php.net/manual/en/function.curl-setopt.php...



    I don't think my PHP document is set to multipart/form-data - Would this affect it?
    A typo has been pointed out to me. curl_setopt ($ch, CURLOPT, $url);
    should be curl_setopt ($ch, CURLOPT_URL, $url);

    This gives me a http 400 error but at least this is an error to go on!

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    32
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by t-bag View Post

    PHP Code:
    $url 'http://127.0.0.1:85/AccountCreator.ashx';
        
    $curl_post_data = array(
        
    "companyName" =>"$companyName",
        
    "mainContact" =>"$mainContact",
        
    "telephone1" =>"$telephone1",
        
    "email" => "$email",
        
    "contact2" => "$contact2",
        
    "telephone2" => '$telephone2',
        
    "email2" => "$email2",
        
    "package" => "$package"
        
    );
        

    curl_setopt ($chCURLOPT_POSTFIELDS$curl_post_data);
    $result curl_exec($ch);
    curl_close($ch); 
    Without Seeing the post form or anything you shouldn't need to run the post array through a foreach loop, But the variables you are posting need to match the value of the form. so
    Code:
     
    <input type="text" value="email" ... >
    you are pulling the value "email" not $email. this should give you a start as i just had a huge headache with curl
    Last edited by dominicdinada; 09-30-2010 at 07:35 AM. Reason: Corrected some missed $

  • #4
    New Coder
    Join Date
    Dec 2009
    Posts
    32
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Also some basic handling to see what errors pop up

    PHP Code:
    if (curl_errno($ch)) {
    echo 
    "Erro CURL: " curl_error($ch);
    } else {    echo 
    $response; }
    curl_close($ch); 


  •  

    Posting Permissions

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