PHP Curl

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
n8lieby
Forum Newbie
Posts: 3
Joined: Thu Apr 22, 2010 6:51 pm

PHP Curl

Post by n8lieby »

Heya,

I've got the following code that is posting to a third party billing site. The problem is that it should be following through to the site's hosted page. Unfortunately, this code is simply barfing the contents of that page back into my site's calling page, rather than merrily forwarding off to the third party. I read that this is commonly due to php being in safe mode or having a locked root, but that’s not the case here, as both of those are disabled when I check phpinfo(). I've got to go through the proxy in order to get out. Could that be what's stopping things? There are no errors in the agent if I check it after the call. Am I missing something obvious?

Code: Select all

                $url = "https://bleh.com/orderform.jsp?";
                $param_string = build_param_string($param);
                $agent = curl_init($url);
                curl_setopt($agent, CURLOPT_PROXY,$proxy_addr);
                curl_setopt($agent, CURLOPT_URL, $url);
                curl_setopt($agent, CURLOPT_HEADER, 0);
                curl_setopt($agent, CURLOPT_POST, count($param));
                curl_setopt($agent, CURLOPT_POSTFIELDS, $param_string);
                curl_setopt($agent, CURLOPT_FOLLOWLOCATION, 1);
                curl_exec($agent);
If I call this from http://mysite.com to https://othersite.com/posttome.jsp, I should end up at https://othersite.com/posttome.jsp, right?

thanks,

-nate
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP Curl

Post by John Cartwright »

n8lieby wrote:Unfortunately, this code is simply barfing the contents of that page back into my site's calling page, rather than merrily forwarding off to the third party.
cURL will perform the request from the server, not the client. Therefore, it is to be expected that either the content will output or return the content as a variable, but never will perform the request on behalf the client.
n8lieby
Forum Newbie
Posts: 3
Joined: Thu Apr 22, 2010 6:51 pm

Re: PHP Curl

Post by n8lieby »

So is there any option to do this from the client?

thanks,

-nate
n8lieby
Forum Newbie
Posts: 3
Joined: Thu Apr 22, 2010 6:51 pm

Re: PHP Curl

Post by n8lieby »

I found this from here:http://netevil.org/blog/2006/nov/http-p ... thout-curl

But it doesn't allow for prox setting like cURL, which is a blocker.

Code: Select all

  
function do_post_request($url, $data, $optional_headers = null)
  {
     $params = array('http' => array(
                  'method' => 'POST',
                  'content' => $data
               ));
     if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
     }
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
     }
     $response = @stream_get_contents($fp);
     if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
     }
     return $response;
  }
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP Curl

Post by John Cartwright »

n8lieby wrote:So is there any option to do this from the client?

thanks,

-nate
PHP is a server side language. If you want to control the client side aspect, you need to use client side languages, such as javascript.
Post Reply