1

Here's my scenario. I have an external URL : https://staging.mead.com.ph/login/sso Now I have to post data to that URL, assuming like the one below :

  1. Name
  2. Type
  3. Mobile Num

Now, after posting I have to be redirected to the [redirect_url] inside curl_getinfo(). [redirect_url] has the following value : https://staging.mead.com.ph/login/key=abcde

HOWEVER,after echoing the result of the curl_exec, it says "Object Moved Here" and "HERE" redirects to https://myservername/login/key=abcde instead of the https://staging.mead.com.ph/login/key=abcde

Here's my code :

$url    =  "https://staging.mead.com.ph/login/key=abcde";
$token  = "token1234";
$params = array(
    "Name" => "Ana",
    "Type" => "A",
    "Mobilenum" => "0919123456"
    );
 foreach($params as $key => $value) { 
    $fields .= $key . '=' . $value . '&'; 
 }
 rtrim($fields, '&');
 $post = curl_init();

 curl_setopt($post, CURLOPT_URL, $url);
 curl_setopt($post, CURLOPT_POST, count($params));
 curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
 curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($post, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($post, CURLOPT_SSL_VERIFYHOST, false); 
 curl_setopt($post, CURLOPT_FOLLOWLOCATION, true);  

 curl_setopt($post, CURLOPT_PROXYUSERPWD, PROXY_AUTH);
 curl_setopt($post, CURLOPT_PROXY, PROXY_URL);
 curl_setopt($post, CURLOPT_PROXYPORT, PROXY_PORT);
 curl_setopt($post, CURLOPT_TIMEOUT,6);

 $result = curl_exec($post);

 echo $result;

 curl_close($post);

What could be wrong? Thanks in advance.

3
  • where is redirection code? Commented May 3, 2016 at 7:45
  • Sorry for this novice question. But do I have to redirect using header? Doesn't curl works like Form Post wherein it is automatically redirected after posting? :) Thanks! Commented May 3, 2016 at 8:24
  • yes, you can do it also. Commented May 3, 2016 at 8:25

1 Answer 1

1

UPDATE

Is there a reason for these:

 curl_setopt($post, CURLOPT_PROXYUSERPWD, PROXY_AUTH);
 curl_setopt($post, CURLOPT_PROXY, PROXY_URL);
 curl_setopt($post, CURLOPT_PROXYPORT, PROXY_PORT);

If so, you need to use the correct parameters, otherwise delete them.

------- End of Update ---------

There are many things that could be going wrong. There is a way to troubleshoot.

add these options:

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

Then get the Headers, response, and stats

$requestHeaderText = curl_getinfo($ch, CURLINFO_HEADER_OUT); 
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
$responseHeaderText = substr($data,0,$skip);
$response = substr($data,$skip);
$stats = curl_getinfo($ch);

In the redirect header you will see a "location", this is where you are being redirected.

You may want to try curl_setopt($post, CURLOPT_POSTFIELDS, $params); this will change the requestContent-Typetomultipart/form-data`

Often cookies are set on the redirect page. Look for SET-COOKIE in the response Headers. If there are cookies then you need to use CURLOPT_COOKIEJAR

Also look for javascript in the response. Sometimes the redirect is done using javascript while performing security checks etc.

You may want to change curl_setopt($post, CURLOPT_FOLLOWLOCATION, true); to false. Then you will only get the headers and stats for the first request.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks! On my header, I noticed that the location is indicated as /login/key=abcde... Maybe that's the reason why I am being redirected to myservername/login/key=abcde instead of staging.mead.com.ph/login/key=abcde... How do I make it redirect to the correct external URL? :(
You must have a typo in your url. mead.com.ph is for sale.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.