SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: CURL?

  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    911
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    CURL?

    I have these,

    Code:
    curl -v -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3N0YWdlLmlkcC5zYWRkbGViYWNrLmNvbS8iLCJhdWQiOiJod42RwOi8vbXlvbmxpbmVzbWFsbGdyb3VwLmNvbS8iLCJuYmYiOjEzODA1NjE1NzYsImV4cCI6MTM4MDU2NTE3Nn0.frMpom25eD2ls0UQh8Jk86bbXv5SOextRdQQfJuOboY" http://stage.api.test.com/Individual?id=1
    So how to code this using PHP CURL?

    Also I'm not sure the -v and -H option of the CURL above?, Is that the JAVA CURL option?

    Thanks in advance.
    I Love coding...
    Don't click this link.

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,079
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solidcodes View Post
    I have these,

    Code:
    curl -v -H "" http://stage.api.test.com/Individual?id=1
    So how to code this using PHP CURL?

    Also I'm not sure the -v and -H option of the CURL above?, Is that the JAVA CURL option?

    Thanks in advance.
    Curl -H is the header and -v is just verbose mode. So essentiall you just need a way to set the header in curl:

    Code PHP:
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://stage.api.test.com/Individual?id=1"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3N0YWdlLmlkcC5zYWRkbGViYWNrLmNvbS8iLCJhdWQiOiJod42RwOi8vbXlvbmxpbmVzbWFsbGdyb3VwLmNvbS8iLCJuYmYiOjEzODA1NjE1NzYsImV4cCI6MTM4MDU2NTE3Nn0.frMpom25eD2ls0UQh8Jk86bbXv5SOextRdQQfJuOboY'
        ));
    if(curl_error($ch)) throw new exception(curl_error($ch));
    print curl_exec($ch);
    curl_close($ch);

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    911
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @K. Wofe ;

    Very very helpful dude.

    I have another Java Curl codes I guess,
    Code:
    curl -u lms:09U1bx6qqHWW8MCIyjQi https://stage.idp.sample.com/issue/simple/?realm=http%3A%2F%2Fmyonlinesmallgroup.com%2F
    So what is -u ?
    And how to do that in PHP curl?
    I Love coding...
    Don't click this link.

  4. #4
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    911
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Okay I get it now, It means,

    u: This specifies the user and password
    I Love coding...
    Don't click this link.

  5. #5
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    911
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I have another questions,

    I have these codes below, and it authenticate the username and password of a user.
    Code:
     	function curl_auth() {
    		$url = "https://stage.idp.samplesite.com/issue/simple/?realm=http%3A%2F%2Fmyonlinesmallgroup.com%2F";  
    		  
    		$ch = curl_init();  
    		  
    		curl_setopt($ch, CURLOPT_URL, $url);  
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    		  
    		// send the username and password  
    		curl_setopt($ch, CURLOPT_USERPWD, "username:password");  
    		  
    		// if you allow redirections  
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
    		// this lets cURL keep sending the username and password  
    		// after being redirected  
    		curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);  
    		  
    		$output = curl_exec($ch);  
    		  
    		echo $output;
    		  
    		curl_close($ch);   		
     	}

    So what happened if the username and password is correct?
    What is the value of $output ?

    Thanks in advance.
    I Love coding...
    Don't click this link.

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,079
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solidcodes View Post
    I have another questions,

    I have these codes below, and it authenticate the username and password of a user.
    Code:
         function curl_auth() {
            $url = "https://stage.idp.samplesite.com/issue/simple/?realm=http%3A%2F%2Fmyonlinesmallgroup.com%2F";  
              
            $ch = curl_init();  
              
            curl_setopt($ch, CURLOPT_URL, $url);  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
              
            // send the username and password  
            curl_setopt($ch, CURLOPT_USERPWD, "username:password");  
              
            // if you allow redirections  
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
            // this lets cURL keep sending the username and password  
            // after being redirected  
            curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);  
              
            $output = curl_exec($ch);  
              
            echo $output;
              
            curl_close($ch);           
         }

    So what happened if the username and password is correct?
    What is the value of $output ?

    Thanks in advance.
    That depends entirely on the remote service and how they handle things like invalid credentials. One thing I see though is that you took out my function checking for curl_error($ch); You need this to capture things such as timeouts.


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
  •