在PHP中使用Curl将get字段附加到URL [英] Attaching get fields to URL using Curl in PHP

查看:87
本文介绍了在PHP中使用Curl将get字段附加到URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用Curl执行服务器和客户端重定向,但是我无法通过get请求将GET字段附加到URL,这是我的代码:

I am able to perform server and client side redirects using Curl but I am unable to attach GET fields to the URL via a get request, here is my code:

$post = curl_init();
curl_setopt($post,CURLOPT_URL,$url);
curl_setopt($post,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($post, CURLOPT_USERAGENT,'Codular');
curl_setopt($post, CURLOPT_CUSTOMREQUEST,'GET');
curl_exec($post);
curl_close($post);

执行执行时没有任何结果,我在做什么错了?

Nothing gets attached when I perform the execution, what am I doing wrong?

我正在使用的新代码:

function curl_req($url, $req, $data='')
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req);
    if (is_array($data)) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
$temp = array("akash"=>"test");
$result = curl_req("http://localhost/test.php", 'POST', $temp);
echo $result;
print_r($result);

test.php:

print_r($_POST);
print_r($_REQUEST);

推荐答案

尝试一下:

// Fill in your DATA  below. 
$data = array('param' => "datata", 'param2' => "HelloWorld");

/*
* cURL request
* 
* @param    $url      string    The url to post to 'theurlyouneedtosendto.com/m/admin'/something'
* @param    $req      string    Request type. Ex. 'POST', 'GET' or 'PUT'
* @param    $data     array     Array of data to be POSTed
* @return   $result             HTTP resonse 
*/
function curl_req($url, $req, $data='')
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req);
        if (is_array($data)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }


// Fill in your URL  below. 

$result = curl_req("http://yourURL.com/?", "POST", $data)
echo $result;

这对我来说很好.

这篇关于在PHP中使用Curl将get字段附加到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆