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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Curl Response Issue

    Server 1 (file1.php) - File Content:
    PHP Code:
     $ch curl_init('www.pasydypremium.com/postme.php');
     
    curl_setopt ($chCURLOPT_POST1);
     
    curl_setopt ($chCURLOPT_POSTFIELDS"email=info@domain.com&username=qaqaqa&password=1111111");
     
    curl_setopt($chCURLOPT_HEADERtrue);
     
    $result curl_exec($ch);
     return 
    $result;
     
    curl_close ($ch); 
    Server 2 (file2.php) - File Content:
    PHP Code:
    $email $_POST['email'];
    $username $_POST['username'];
    $password $_POST['password'];
    //check for duplicate
    $duplicate = @mysql_num_rows(@mysql_query("SELECT email FROM table WHERE email='$email'"));

    if(
    $duplicate == 0){
    //Add email into our database
    $result = (int)$query = @mysql_query("INSERT INTO users2 (email) VALUES ('$email')");
    if(
    $result){
    $error "Email Inserted";
    }else{
    $error "Query Error";
    }
    }else{
    $error "Email Duplilcate";

    My Question is, i want the second server (file2.php) to send the first server (file1.php) the $error message:
    Case 1: email was inserted successfully
    Case 2: query could not be excuted
    Case 3: Duplicate found

    how can then be done?

    Thank you.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Not sure I understand the relationship between these two files.

    Since the curl code is literally all hard coded from what is in file1, you can simply include it as necessary into file2, and it will have direct access to the $error variable to do with whatever you have to do. Otherwise, you can put it with file_get_contents and provide it with an http wrapper and a querystring with the $error, or even use curl once again to connect to file1.php. I'd opt for the include myself.

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Basically, we have provided our client file1.php to be located into their server and file2.php is located on our server, every time the clients add an email to their database, file1.php is executed and the email is send to us, our database must also include a copy of the email.

    We are looking for a solution!! Client server must send us the email and we must send them back the response.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    So why are you using cURL to post to postme.php instead of file2.php?

    Am I missing something? ???

    As for the result of your call to file2.php OR postme.php, it should be contained in the $result variable from file1.php.. if I'm understanding your code and principle correctly and seeing as you're referring to file2.php and postme.php I'm not entirely sure I am.

    Confused? Imagine how I feel!
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    So why are you using cURL to post to postme.php instead of file2.php?

    Am I missing something? ???

    As for the result of your call to file2.php OR postme.php, it should be contained in the $result variable from file1.php.. if I'm understanding your code and principle correctly and seeing as you're referring to file2.php and postme.php I'm not entirely sure I am.

    Confused? Imagine how I feel!
    Sorry I just forgot to rename “postme.php” to “file2.php” on this thread, the script is working fine, file2.php is receiving the email and is being inserted into our database, and the problem comes with the response messages.

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by eve8080 View Post
    the problem comes with the response messages.
    So what does that mean? You're not getting them at file1.php, you're getting blank messages, PHP error messages?

    You're not really telling us what your problem is in order for us to help.

    You might want to print $result so that the url request can get the text.. I don't see a print command anywhere.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I think I see what you are doing here.
    Add a return transfer for the curlopt: curl_setopt(CURLOPT_RETURNTRANSFER, 1);. Now this file will be able to pull the results of the curl_exec call and store it in a variable.
    Then with the script its connecting to, just output what is required. If this is exactly what the second script uses, I'd actually return the results as integers and handle it in the receiving file1 script. So on success, print 0, failure print 1. And for a sql error such as a duplicate, I'd push something different.

    Now, this is if you want to use curl. This is actually a perfect example of the use of soap services. It takes some work to set up soap server and client's properly, but when you do they sure become easy. What happens here is the client file1 would establish a soap client connection to the remote server. The remote server would present it with a method called say, insertEmailAddress. This method is called much like a regular PHP function, and the results can be captured as a part of the soap protocols for returning. Super easy to use. Soap is somewhat tough to set up on PHP I find.

  • #8
    New Coder
    Join Date
    Sep 2012
    Posts
    71
    Thanks
    0
    Thanked 8 Times in 8 Posts
    You could also roll your own XML if you were hesitant to deal with soap. Just define a private protocol and build the string...

    <response>
    <username>x</username>
    <duplicate>0</duplicate>
    <email>nobody@nowhere.no</email>
    </response>

    Be sure to follow encoding rules for the values though.
    I'm sure some folks around here would be happy to get more involved if people posted tasks here and offered a few bucks.

    Beware, code snippets in my posts are meant as examples... they certainly may contain typos.


  •  

    Posting Permissions

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