简体   繁体   中英

Using PHP CURL to post data to external URL and redirect

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.

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 request curl_setopt($post, CURLOPT_POSTFIELDS, $params); this will change the request Content-Type to multipart/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM