www.webdeveloper.com
Results 1 to 10 of 10

Thread: PHP / cURL Question

  1. #1
    Join Date
    Sep 2010
    Posts
    30

    PHP / cURL Question

    G'day,

    Just looking for some assistance from the community at large.

    Scenario:

    Currently trying to develop a small app using a simple form in HTML / PHP and will will need some assistance with cURL (whatever this is).

    Here's the HTML:

    <html>
    <body>
    <form action="receiving_parameters.php" method="post">
    Name: <input type="text" name="name"><br>
    <input type="text" name="action" type="hidden">action<br>
    <input type="submit">
    </form>
    </body>
    </html>

    posting receiving_parameters.php (no problem here)

    Here's where I'm lost:

    I have to take the passed parameters I then have to take the parameters and using an API key, pass this info using cURL to a service (TROPO) to send a text message. I have no clue what cURL is and no idea how to pass these parameters using it either.

    Any advice or help would be appreciated and might just include a 2fer of your favorite suds, FedEx'd next day.

    Cheers!

  2. #2
    Join Date
    Sep 2013
    Posts
    221
    cURL, a library that allows PHP to get information from another server. This is primarily useful for APIs (Application Programming Interfaces)

    //extract data from the post
    extract($_POST);
    //set POST variables
    $url = 'http://domain.com/get-receiving_parameters.php';
    $fields = array(
    'name' => urlencode($last_name),

    );

    //url-*** the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    //open connection
    $ch = curl_init();
    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    //execute post
    $result = curl_exec($ch);
    //close connection
    curl_close($ch);

    You can follow with the below link to get to knw more about cURL:
    http://php.net/manual/en/book.curl.php

    cURL is a library that lets you make HTTP requests in PHP.
    Hope this helps.
    strad solutionswww.stradsolutions.com

  3. #3
    Join Date
    Sep 2010
    Posts
    30

    PHP/cURL Question

    So If I'm readthing this correctly and of course my understanding of it:

    //extract data from the post
    extract($_POST);

    -- This will extract the data i'm gathering from the end user. In this case, their name, area code, phone number and a text message. i.e.:

    <input type="text" placeholder="Your Full Name:" class="text requiredField transparent_class" id="name" name="name" size="14px" value="" />
    <input type="text" placeholder="Area Code:" class="text requiredField transparent_class" id="phoneac" name="phoneareacode" size="7px"value="" />
    <input type="text" placeholder="Phone Number:" class="text requiredField transparent_class" id="phonenum" name="phonenum" size="11px"value=""/>
    <input type="text" placeholder="Your Text Message (60 characters maximum):" class="text requiredField transparent_class" name="maxcharfield" id="maxcharfield" size="36px" value="" onKeyDown="textCounter(this,'progressbar1',60)"

    name, phoneareacode, phonenum, maxcharfield



    extract($_POST);
    //set POST variables
    $url = 'http://domain.com/get-receiving_parameters.php'; - this is from the form action: <form action="get-receiving_parameters.php" method="post" name="form" id="form">
    $fields = array(
    'name' => urlencode($last_name),

    --

    $fields = array(
    'name' => urlencode($name, $phoneareacode, $phonenum, $maxcharfield),


    //url-*** the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } - iterate over the contents

    - { $fields_string .= $key.'='.$value.'&'; } --- not certian what this is doing

    - rtrim($fields_string, '&'); - stripping the contents of any white space


    == LOST ===

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

    //execute post
    $result = curl_exec($ch);
    //close connection
    curl_close($ch);

    Also, how does the API key fit into this?

    Again, thanks for the info..

    Quote Originally Posted by priyankagound View Post
    cURL, a library that allows PHP to get information from another server. This is primarily useful for APIs (Application Programming Interfaces)

    //extract data from the post
    extract($_POST);
    //set POST variables
    $url = 'http://domain.com/get-receiving_parameters.php';
    $fields = array(
    'name' => urlencode($last_name),

    );

    //url-*** the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    //open connection
    $ch = curl_init();
    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    //execute post
    $result = curl_exec($ch);
    //close connection
    curl_close($ch);

    You can follow with the below link to get to knw more about cURL:
    http://php.net/manual/en/book.curl.php

    cURL is a library that lets you make HTTP requests in PHP.
    Hope this helps.

  4. #4
    Join Date
    Sep 2013
    Posts
    221
    you would need to include the API key (password) within your code in order to gain access to the API.

    You can use API in your header or in url.
    HTTP headers need to be specified using a colon as the separator between the key and the value, not an equal sign. Try this:

    $header = array('Content-Type: application/xml', 'API-KEY: 1a2b3c4d5e6f7g8h9i', 'API-LOGIN: 1a2b3c4d5e6f7g8h9i');

    hope this helps.
    strad solutionswww.stradsolutions.com

  5. #5
    Join Date
    Sep 2010
    Posts
    30

    PHP / cURL Question

    Quote Originally Posted by priyankagound View Post
    you would need to include the API key (password) within your code in order to gain access to the API.

    You can use API in your header or in url.
    HTTP headers need to be specified using a colon as the separator between the key and the value, not an equal sign. Try this:

    $header = array('Content-Type: application/xml', 'API-KEY: 1a2b3c4d5e6f7g8h9i', 'API-LOGIN: 1a2b3c4d5e6f7g8h9i');

    hope this helps.
    At it's most basic form: minus the token.

    <?php
    $token = '';
    $numbertodial = '16479627170';
    $msg = 'test+Tropo+message';

    $curl_handle = curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,'http://api.tropo.com/1.0/sessions?action=create&token='.$token.'&numbertodial='.$numbertodial.'&msg='.$msg);
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    curl_exec($curl_handle);
    curl_close($curl_handle);
    ?>

  6. #6
    Join Date
    Sep 2010
    Posts
    30
    At it's most basic form: minus the token.

    <?php
    $token = '';
    $numbertodial = '16479627170';
    $msg = 'test+Tropo+message';

    $curl_handle = curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,'http://api.tropo.com/1.0/sessions?action=create&token='.$token.'&numbertodial='.$numbertodial.'&msg='.$msg);
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    curl_exec($curl_handle);
    curl_close($curl_handle);
    ?>

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    Quote Originally Posted by rel82me View Post
    So If I'm readthing this correctly and of course my understanding of it:

    //extract data from the post
    extract($_POST);

    -- This will extract the data i'm gathering from the end user. In this case, their name, area code, phone number and a text message. i.e.:
    ...
    Make sure you really understand what extract() does, and the potential negative side effects. For better security, I recommend you not use it and just reference the $_POST (or $_GET, etc.) elements you need when/where you need them. Not only is it safer, but it makes it immediately obvious in the source code what the source of those values is.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Sep 2010
    Posts
    30

    PHP / cURL questions

    Quote Originally Posted by NogDog View Post
    Make sure you really understand what extract() does, and the potential negative side effects. For better security, I recommend you not use it and just reference the $_POST (or $_GET, etc.) elements you need when/where you need them. Not only is it safer, but it makes it immediately obvious in the source code what the source of those values is.

    Hey all, it's working. thanks so much for all your help! I can take it from here!

  9. #9
    Join Date
    Sep 2010
    Posts
    30
    Hey everyone thank you so much. One more question. Can cURL redirect a URL? I'm receiving a "successs" message and a text-message does pass through the the Tropo API framework, but I',m not certian how to handle the boolean to redirect to a simple "thanks" page.

  10. #10
    Join Date
    Sep 2010
    Posts
    30
    Hey everyone thank you so much. One more question. Can cURL redirect a URL? I'm receiving a "successs" message and a text-message does pass through the the Tropo API framework, but I',m not certian how to handle the boolean to redirect to a simple "thanks" page.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles