如何在curl php中传递url值

问题描述 投票:0回答:4
$description = "some test data and url";
$description .="http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow";

curl_setopt($sch, CURLOPT_URL, "myserverurl");
curl_setopt($sch, CURLOPT_HEADER, 0);             
curl_setopt($sch, CURLOPT_POST, true);
curl_setopt($sch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($sch, CURLOPT_POSTFIELDS, "orgid=$orgid&description=$description&external=1");
curl_setopt ($sch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($sch, CURLOPT_SSL_VERIFYPEER, 0); 

当我检查服务器(myserverurl)。

我可以看到描述字段

“一些测试数据和网址http://www.mydata.com?test=1”。

在'&'之后我丢失了描述

是的,我们可以在发送curl之前对网址进行编码,但是我没有权限在该第三方api服务器上再次解码网址

php curl urlencode
4个回答
0
投票

如果你urlencode你发送的每个参数的值怎么办?

您不必担心另一方面的解码:它是通过GET / POST发送数据的标准方式

就像是 :

curl_setopt($sch, CURLOPT_POSTFIELDS, 
    "orgid=" . urlencode($orgid) 
    . "&description=" . urlencode($description) 
    . "&external=1"
);

如果这不起作用,请尝试使用rawurlencode? (如果我没记错的话,空格有区别)


0
投票

简单。您可以在输入后简单地获取当前URL。

所以,如果你进入

http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow

(我认为,mydata.com是你的服务器),你可以很容易地回应它:

$url = str_replace("?","",$_SERVER['REQUEST_URI']);
echo $url;

以上回声应该给出:

test=1&user=4&destination=645&source=stackoverflow

从那时起,您可以简单地将整个字符串保存到数据库中,或者通过either $_SERVER['test']保存单个变量(测试,用户,目标,源),或者,如果它们中的一些变化,您可以通过&字符将它们分解以保存它们动态。


0
投票

将post的数据放入数组中,并将该数组用作CURLOPT_POSTFIELDS。您可以在$_POST['description']中获得描述。

例 test.php的

<?php
$sch = curl_init(); 
$post_data=array();
$orgid='testorg';
$description = "some test data and url";
$description .="http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow";
$post_data['description']=$description;
$post_data['orgid']=$orgid;
$post_data['external']=1;
curl_setopt($sch, CURLOPT_URL, "localhost/testcurl.php");
curl_setopt($sch, CURLOPT_HEADER, 0);             
curl_setopt($sch, CURLOPT_POST, true);
curl_setopt($sch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($sch, CURLOPT_POSTFIELDS, $post_data);
$re=curl_exec($sch);

echo('<pre>');
echo($re);

testcurl.php

<?php
var_dump($_POST);

结果

array(3) {
  ["description"]=>
  string(94) "some test data and urlhttp://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow"
  ["orgid"]=>
  string(7) "testorg"
  ["external"]=>
  string(1) "1"
}

0
投票

这是一个简单的解决方案。如果你正在使用php使用函数curl_escape()`

$msg=$_POST['Message'];

$ch = curl_init();
$new = curl_escape($ch, $msg);


$ch = curl_init("url".$new."/xyz.com");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
© www.soinside.com 2019 - 2024. All rights reserved.