This question already has answers here:
curl POST format for CURLOPT_POSTFIELDS
(10 answers)
Closed 4 years ago.
I have this curl request below, which was successfully troubleshooted in another post. Right now, my PHP seems to work through this code without any errors, moves onto the next part of the IF statement and sends the confirmation email. It just doesn't update the database as it should from the web service. I will have to email the creator of the web service if this does not work but I just want to be sure that the code is fairly solid before I do this. Any one have any ideas? Here is the code:
$url = 'http://127.0.0.1:85/AccountCreator.ashx';
$curl_post_data = array(
'companyName' =>urlencode($companyName),
'mainContact' =>urlencode($mainContact),
'telephone1' =>urlencode($telephone1),
'email' => urlencode($email),
'contact2' => urlencode($contact2),
'telephone2' => urlencode($telephone2),
'email2' => urlencode($email2),
'package' => urlencode($package)
);
foreach($curl_post_data as $key=>$value) {$fields_string .=$key. '=' .$value.'&';
}
rtrim($fields_string, '&');
//die("Test: ".$fields_string);
$ch = curl_init();
curl_setopt ($ch, CURLOPT, $url);
curl_setopt ($ch, CURLOPT_POST, count($curl_post_data));
curl_setopt ($ch, CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
Firstly, what is the problem? It would be easier to troubleshoot it if you explained exactly what the failure in the code was. Secondly, there are a couple of odd things you are doing in this code:
I don't see why you are doing
curl_setopt ($ch, CURLOPT_POST, count($curl_post_data));
CURLOPT_POST requires a boolean (true/false) setting. You should set it to true.
Secondly, you don't need to encode CURLOPT_POSTFIELDS manually. Make an array and let cURL deal with it internally:
$curl_post_data = array(
'companyName' =>$companyName,
'mainContact' =>$mainContact,
'telephone1' =>$telephone),
'email' => $email,
'contact2' => $contact2,
'telephone2' => $telephone2,
'email2' => $email2,
'package' => $package
);
These might not fix the problem, but they may help.
The CURLOPT_POSTFIELDS option accepts an associative array of POST-data. Probably better to use that one rather than to construct the query string yourself so you got someone else to blame when it blows up.
PHP Manual:
The full data to post in a HTTP "POST"
operation. To post a file, prepend a
filename with # and use the full path.
This can either be passed as a
urlencoded string like
'para1=val1¶2=val2&...' or as an
array with the field name as key and
field data as value. If value is an
array, the Content-Type header will be
set to multipart/form-data.
Related
I'm new to StackOverflow, so I apologize if I'm not formatting this correctly. I'm using the GitHub API and my goal is to get a list of a user's repositories in a dropdown form that they can select from.
Let's say the repository list URL is https://api.github.com/users/MY_GITHUB_USERNAME/repos (The way I sat things up I can get the repo URL by doing $userdata->repos_url). When I use the following:
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_URL, $userdata->repos_url);
curl_setopt($curl1, CURLOPT_HEADER, 0);
curl_setopt($curl1, CURLOPT_HTTPHEADER, array(
'User-Agent: MY_WEBSITE_HERE Addon Developer OAuth'
));
curl_setopt($curl1,CURLOPT_USERAGENT,'User-Agent: MY_WEBSITE_HERE Addon Developer OAuth');
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl1, CURLOPT_SSL_VERIFYHOST, 0);
$cont1 = curl_exec($curl1);
curl_close($cont1);
echo $cont1;
It responds with the following:
[
{
(information I don't need)
"full_name": "github-username/this-is-what-i-want",
(information I don't need)
}
]
I only have one repository at the moment. What I want to do is make a code that echos only the full_name and if there's more than one array echo each one. (All arrays will have full_name.)
Does anyone know how I could do this?
Decode it to an array, then loop through the arrays until you get to what you want:
$data=json_decode($cont1, true); <~~~ tells php to decode the JSON into an array
$results=$data['FirstArray']['SecondArray']['NumResultsReturned']; <~~ most JSON's have a value showing how many arrays got sent back to you in the results. You didn't give a true data example as a reply, so can't give exacts on these fields for you.
I have a cookie file with this
.adidas.de TRUE / FALSE 1519852113 restoreBasketUrl %2Fon%2Fdemandware.store%2FSites-adidas-DE-Site%2Fde_DE%2FCart-UpdateItems%3Fpid_0%3DBB2092_610%26qty_0%3D1
.adidas.de TRUE / FALSE 1550524169 utag_main v_id:015fe2df50a60014c3cedb252f0d04073001906b01978$_sn:7$_ss:0$_st:1518989969891$_prevpage:ACCOUNT%7COVERVIEW%3Bexp-1518991769885$_pn:3%3Bexp-session$ses_id:1518987220846%3Bexp-session$ab__doubleclick:TEST%3Bexp-1521580164884
I know I can read it out from file in cURL like
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookiefile);
But how can I set these cookies manual without a file? Because I need to modify them a bit before sending
You can use curl_setopt function with option CURLOPT_HTTPHEADER.
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Cookie: mycookie=value;mycookie2=value2"]);
[Edit]
From this guide The Unofficial Cookie FAQ.
The layout of Netscape's cookies.txt file is such that each line contains one name-value pair. An example cookies.txt file may have an entry that looks like this:
.netscape.com TRUE / FALSE 946684799 NETSCAPE_ID 100103
Each line represents a single piece of stored information. A tab is inserted between each of the fields.
From left-to-right, here is what each field represents:
domain - The domain that created AND that can read the variable.
flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. This value is set automatically by the browser, depending on the value you set for domain.
path - The path within the domain that the variable is valid for.
secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
expiration - The UNIX time that the variable will expire on. UNIX time is defined as the number of seconds since Jan 1, 1970 00:00:00 GMT.
name - The name of the variable.
value - The value of the variable.
For answer your question
1) To modify a bit before sending do this:
$file = file('cookie.txt');
$cookies = [];
/*
* From the guidelines that I linked and posted above each line must contain 7 "fields" separated by tab only (The text you posted in the question is not well formatted but with some tricks we can do this)
*/
foreach ($file as $fileLine) {
$fileLine = preg_replace("/\s+/", "\t", trim($fileLine));
$fields = explode("\t", $fileLine);
if (count($fields) === 7) {
$cookies[] = [
'domain' => $fields[0],
'flag' => $fields[1],
'path' => $fields[2],
'secure' => $fields[3],
'expiration' => $fields[4],
'name' => $fields[5],
'value' => $fields[6],
];
}
}
var_dump($cookies); // Here you have all lines from your cookie file and can modify the fields such name, value or something
2) For override the cookie.txt with your modify cookies array do this:
// Preserve Netscape format
$cookies = implode(PHP_EOL, array_map(function($data){
return implode("\t", $data);
}, $cookies));
file_put_contents('cookie.txt', $cookies);
3) For send your new cookies from file do this (must be in Netscape format):
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
Hello: I have some code in on file and it needs to redirect to a WP page and carry some variables with it. Now I know I can do this with a "printf" statement and add some query string variables on the end of the URL of the page. But I do not want the variables up in the URL. I know if it was a form - it could be done with GET or POST variables. And post variables are not up in the URL. But the only way I know how to do post variables is with a form. I cannot use a form for this that needs a "submit" button that needs to be pressed. It needs to be something that is automatically executed when the script is loaded like a printf statement would be. Does anyone have any ideas on how this could be done ?
Thanks, Gerard
******* update 12/13 1:32PM EST ***************
Hello Arkascha - I have attempted to do what you suggested in your comment, and created the Curl call along with the post variables. But it would not actually redirect to the page. Not sure if I just did it wrong or if it cannot do that. But i was researching and saw where people did a header redirect along with the CURL. And so i thought i would try that. What I have does do the redirect; but it does not seem to carry the post variables with it. So I am not sure if something is wrong with my CURL code, or whether or not I need a header redirect.... etc etc...
here is my code:
$buy_refi='purchase';
$to_borrow=$entry["58"];
$home_worth=$entry["57"];
$down_payment=$entry["62"];
$purchase_price=$entry["54"];
$credit=$entry["15"];
$data = array(
'buy_refi' => $buy_refi,
'to_borrow' => $to_borrow,
'home_worth' => $home_worth,
'down_payment' => $down_payment,
'purchase_price' => $purchase_price,
'credit' => $credit,
);
$data = http_build_query($data); // convert array to urlencoded string
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://www.mydomain.dev/purchase-options/',
CURLOPT_POST => true,
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$resp = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
header("Location:http://www.mydomain.dev/purchase-options/");
*********** here is the code on redirect page which will receive post variables...
$_POST['input_2']=$_POST['buy_refi'];
$_POST['input_58']=$_POST['to_borrow'];
$_POST['input_57']=$_POST['home_worth'];
$_POST['input_62']=$_POST['down_payment'];
$_POST['input_54']=$_POST['purchase_price'];
$_POST['input_15']=$_POST['credit'];
echo "post buy refi: ".$_POST['buy_refi'];
echo "<br/>";
echo "<br/>";
I tried implementing the following PHP code to POST JSON via PHP: cURL (SOME FORCE.COM WEBSITE is a tag that signifies the URL that I want to POST):
$url = "<SOME FORCE.COM WEBSITE>";
$data =
'application' =>
array
(
'isTest' => FALSE,
key => value,
key => value,
key => value,
...
)
$ch = curl_init($url);
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POST, true);
//Send blindly the json-encoded string.
//The server, IMO, expects the body of the HTTP request to be in JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array
(
'Content-Type:application/json',
'Content-Length: ' . strlen($data_string)
)
);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
echo '<pre>';
echo $_POST;
$jsonStr = file_get_contents('php://input'); //read the HTTP body.
var_dump($jsonStr);
var_dump(json_decode($jsonStr));
echo '</pre>';
The output of the above is the following:
"Your TEST POST is correct, please set the isTest (Boolean) attribute on the application to FALSE to actually apply."
Arraystring(0) ""
NULL
OK, the above confirms that I formatted the JSON data correctly by using json_encode, and the SOME FORCE.COM WEBSITE acknowledges that the value of 'isTest' is FALSE. However, I am not getting anything from "var_dump($jsonStr)" or "var_dump(json_decode($jsonStr))". I decided to just ignore that fact and set 'isTest' to FALSE, assuming that I am not getting any JSON data because I set 'isTest' to TRUE, but chaos ensues when I set 'isTest' to FALSE:
[{"message":"System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing body, need at least one of html or plainText: []\n\nClass.careers_RestWebService.sendReceiptEmail: line 165, column 1\nClass.careers_RestWebService.postApplication: line 205, column 1","errorCode":"APEX_ERROR"}]
Arraystring(0) ""
NULL
I still do not get any JSON data, and ultimately, the email was unable to be sent. I believe that the issue is resulting from an empty email body because there is nothing coming from "var_dump($jsonStr)" or "var_dump(json_decode($jsonStr))". Can you help me retrieve the JSON POST? I would really appreciate any hints, suggestions, etc. Thanks.
I solved this question on my own. I was not sure if I was doing this correctly or not, but it turns out that my code was perfect. I kept refreshing my website, from where I am POSTing to SOME FORCE.COM WEBSITE. I believe that the people managing SOME FORCE.COM WEBSITE were having issues on their end. Nothing was wrong with what I did. For some reason, I got a code 202 and some gibberish text to go along with it. I would be glad to show the output, but I do not want to POST again for the sake of the people managing SOME FORCE.COM WEBSITE that I am POSTing to. Thank you guys for your help.
I'm not sure about what encoding to use when using cURL:
GET:
(URL=) http://www.example.com/form.php?test=test+1
(URL=)
http://www.example.com/form.php?test=test%201
POST:
(POSTFIELDS=) test=test 1
(POSTFIELDS=) test=test+1
(POSTFIELDS=) test=test%201
CURL can accept an array of arguments for post, and it'll take care of the encoding for you:
$array = (
'test' => 'test 1',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
However, as per the curl docs (http://php.net/curl_setopt, search for CURLOPT_POST_FIELDS), for PHP, the pairs should be in urlencode() format:
$post_args = urlencode('test=test 1');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_args);