Send post with curl php - php

I'm trying to send some parameters for an autentication on a web page this is the code
$url = "http://www.webpage.com/account/submit";
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $url);
curl_setopt($handler, CURLOPT_POST,true);
curl_setopt($handler, CURLOPT_POSTFIELDS, "username=user#webmail.com&remember=true&password=123456");
$response = curl_exec ($handler);
curl_close($handler);
When I run this script shows nothing, Im trying to autenticate me succesfully into a website without the HTML form . . . there is another way to do this ???

Ofcourse it shows nothing. You would at least have to echo something (and likely do something with $response to actually check the result of the login). Also, make sure error reporting is enabled.

Related

Php using Curl to POST to a custom API

I'm working on a project and am attempting to link a Java application to my website/database via an API.
I'm running on an aws ec2 instance. The problem I am encountering is using Curl to POST data to another webpage. (The end-product will require proxies, and I've decided to use curl to manage this.)
The PHP code that makes the request:
<?php
function get_url($url)
{
$ch = curl_init();
if($ch === false)
{
die('Failed to create curl object');
}
// Temporary, just trying to get POST value to work.
$data = "password=temp";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data2 = curl_exec($ch);
curl_close($ch);
return $data2;
}
echo get_url("www.mywebsite.org/API/myscript.php");
?>
The server-side code that handles the request:
<?php
echo "Password: " . $_POST["password"];
?>
The issue I am having is with passing the post variables through curl and retrieving them.
The output on the page is:
Password:
and the POST variables are not being set. I'm thinking I am missing a CURLOPT somewhere but can't figure out which one!

How to access odata that produced by Nav webservice into html layout using PHP

I try to get odata produced by a navision webservice. When I directly access the url given using chrome browser, the page asks for user name and password and then chrome shows xml data as expected.
But when i use a PHP script, it always returns "1".
My code looks like this:
$url = 'http://103.7.1.182:14048/DynamicsNAV71-6/OData/Company(\'Unit%20G%205\')/Item_Master_on_hand_no_Desc';
$login = 'Gem-gae\senzo1:Bsbsenzo2018';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/x-www-form-urlencoded',
'Content-Length: ' . strlen(1))
);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
Is there any mistakes with that code ?
Thanks for your help
There's no mistake on your code, it successfully connects to the external server via cURL and retrieves the data.
The value in $output (if you do a var_dump) is true (that's why you see 1 if you echo it).
Said that, the issue is on the server you are contacting (103.7.1.182:14048).
You probably need to send some data or something, I can't tell you exactly what because I don't know what is in that server.
To send data, you can add a curl_setopt line:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
Where $data is an array.

How to use PHP and curl to execute a POST call

I am trying to use curl with PHP to execute a POST call from PHP but always I get page error
this is my code
if($curl = curl_init()){
curl_setopt($curl, CURLOPT_URL, 'http://marcelotavarez.com/freeodds.aspx');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, "CallbackMethod=GetLines&Parm1=20170110&Parm2=2&Parm3=0&CallbackParmCount=3&__WW EVENTCALLBACK=CustomerCallback&__WWEVENTTARGET=Page&__VIEWSTATE=/wEPDwUKLTQwNjM5NzY1NmRkOa+UqFoVFF+BzUdMUBBH1ahGsWQ=&__VIEWSTATEGENERATOR=BB49A8FA&");
$out = curl_exec($curl);
echo "===\n $out \n====\n";
$out = str_replace("\"","",$out);
}
Using the inspector from Chrome I checked the header that I need to use to do the POST Call and these are:
CallbackMethod:GetLines
Parm1:"20170110"
Parm2:2
Parm3:0
CallbackParmCount:3
__WWEVENTCALLBACK:CustomerCallback
__WWEVENTTARGET:Page
__VIEWSTATE:/wEPDwUKLTQwNjM5NzY1NmRkOa+UqFoVFF+BzUdMUBBH1ahGsWQ=
__VIEWSTATEGENERATOR:BB49A8FA
Any time that I use this code just I get the error page
Any cluees or any help , pls
TIA
I need to ask you a question where is CallbackMethod=GetLines
GetLines function , I think must be in your code to be executed after response back

Does Marketo API block curl on a per account basis?

I am trying to connect to the Marketo.com REST API using curl.
I can't get a response from the identity service. I only get an error message
"[curl] 6: Couldn't resolve host 'MY_CLIENT_ENDPOINT.mktorest.com'
,
but I can print the constructed url and paste it into a browser address bar and this will provide the expected response with the access_token element.
I can use curl in php and in a terminal to access my gmail account so curl is able to access an https service.
I have tried sending the parameters in the curl url as a get request and also by declaring them with curl's -F option as a post request
My application uses dchesterton/marketo-rest-api available on github, but I have also tried a simple php curl request just to get the access token.
private function getToken() {
$url = "$this->client_url/identity/oauth/token?grant_type=client_credentials&client_id=$this->client_id&client_secret=$this->client_secret";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$errors = curl_error($ch);
curl_close($ch);
file_put_contents($this->logDir . 'access_token_response' . date('Y-m-d') . '.txt', $url . "\n" . $response . "\n", FILE_APPEND);
if ($errors) {
file_put_contents($this->logDir . 'access_token_errors' . date('Y-m-d') . '.txt', $errors . "\n", FILE_APPEND);
}
return $response['access_token'];
}
Again, this fails with the same error but produces a perfectly formed url that I can paste into the browser and get a valid response.
I have also tried this using post instead of get as I have for every other test mentioned, and these have been tried on my localhost and on a test server.
Can anyone explain to me why this would fail?
Does Marketo block curl on a per account basis?
I was trying to implement something similar but my code wasn't working. I'm not sure exactly what is failing but I tried your code and it seems to work perfectly after some slight modifications:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($request_data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($curl);
$errors = curl_error($curl);
curl_close($curl);
I hope this helps.

using curl with php

I have an sms android app that works remotely using a http server, It need to get a formed url request like this :
http://server.com:9090/sendsms?phone=123456789&text=foobar&pass=123456
When i type that url in the browser address bar and hit enter, the app sends the sms.
I'm new to curl, and I dont know how to test it, here is my code so far:
$phonenumber= '12321321321'
$msgtext = 'lorem ipsum'
$pass = '1234'
$url = 'http://server.com:9090/sendsms?phone=' . urlencode($phonenumber) . '&text=' . urlencode($msgtext) . '&password=' . urlencode($pass);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
So my questions are, is the code correct? and how to test it?
Altough this is a simple GET, I cannot fully agree with hek2mgl. There are many situations, when you have to take care of timeouts, http response codes, etc. and this is what cURL is for.
This is a basic setup:
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $url);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true);
// curl_setopt($handler, CURLOPT_MAXREDIRS, 10); // optional
// curl_setopt($handler, CURLOPT_TIMEOUT, 10); // optional
$response = curl_exec($handler);
curl_close($handler);
If you can access the url using the address bar in browser, then it is a HTTP GET request. The simplest thing to do that in PHP would be using file_get_contents() since it can operate on urls as well:
$url = 'http://server.com:9090/sendsms?phone=123456789&text=foobar&pass=123456';
$response = file_get_contents($url);
if($response === FALSE) {
die('error sending sms');
}
// ... check the response message or whatever
...
Of course you can use the curl extension, but for a simple GET request, file_get_contents() will be the simplest and most portable solution.

Categories