I've recieved the following data which I need to post to server
and I cant find out how exactly I need to do it in PHP.
I need to send numbers, but I cant see any parameter thier asking me to send it from.
any ideas how I could send this data?
AFAIK you need this:
$postdata = file_get_contents("php://input");
Details:
http://php.net/manual/en/wrappers.php.php
Or maybe the other way:
RAW POST using cURL in PHP
(I am bit lost in your question.)
The main idea there:
curl_setopt($ch, CURLOPT_POSTFIELDS, "numbers\nnumbers\n..." );
Related
I am creating a PHP package that I want anyone to be able to use.
I've not done any PHP dev in a few years and I'm unfamiliar with pear and pecl.
The first part of my question is related to Pecl and Pear:
It seems to me that Pear and pecl are updating my computer, rather than doing anything to my code base, which leads me to the assumption that anything I do with them will also need to be duplicated by anyone wanting to use my package. Is that correct?
The 2nd part of my question is specific, I just want to do a simple HTTP (POST) request, and ideally I'd like to do it without any config required by those who use my package.
These are options I'm considering :
HTTPRequest seems like the perfect option, but it says "Fatal error: Uncaught Error: Class 'HttpRequest' not found" when I try and use it out of the box, and when I follow these instructions for installing it I get, "autoheader: error: AC_CONFIG_HEADERS not found in configure.in
ERROR: `phpize' failed" -- I don't want to debug something crazy like that in order to do a simple HTTP request, nor do I want someone using my package to have to struggle through something like that.
I've used HTTP_Request2 via a pear install and it works for me, but there is nothing added to my codebase at all, so presumably this will break for someone trying to use my package unless they follow the same install steps?
I know that I can use CURL but the syntax for that seems way over the top for such a simple action (I want my code to be really easy to read)
I guess I can use file_get_contents() .. is that the best option?
and perhaps I'll phrase the 2nd part of my question as :
Is there an approach that is considered best practice for (1) doing a HTTP request in PHP, and (2) for creating a package that is able to be easily used by anyone?
This really depends on what you need your request for. While it can be daunting when first learning it, I prefer to use cURL requests most of the time unless all I need to do is query the page with no headers. It becomes pretty readable once you get used to the syntax and the various options in my opinion. When all I need to do is query a page with no headers, I will usually use file_get_contents as this is a lot nicer looking and simpler. I also think most PHP developers can agree with me on this standpoint. I recommend using cURL requests as, when you need to set headers, they're very organized and more popular than messing with file_get_contents.
EDIT
When learning how to do cURL in PHP, the list of options on the documentation page is your friend! http://php.net/manual/en/function.curl-setopt.php
Here's an example of a simple POST request using PHP that will return the response text:
$data = array("arg1" => "val1", "arg2" => true); // POST data included in your query
$ch = curl_init("http://example.com"); // Set url to query
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // Send via POST
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // Set POST data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response text
curl_setopt($ch, CURLOPT_HEADER, "Content-Type: application/x-www-form-urlencoded"); // send POST data as form data
$response = curl_exec($ch);
curl_close($ch);
I have been asked to deal with web hook concept. I am very new to this concept and i will need your help. I was asked to provide a URL to a company so they can send json data from their website.
So far i found this :
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
so is URL same as this PHP file? I am really confused. Thank You
Update: I have created an article to help with this
You don't need to use curl for a HubSpot webhook, the webhook should trigger some kind of action on your server and receive posted data ready for massaging and manipulating. A webhook could be, when a contact fills out a form or completes a specific action within HubSpot and you push that data to your custom script on a stand alone server. You should start with how to handle JSON data in a POST request. http://edwin.baculsoft.com/2011/12/how-to-handle-json-post-request-using-php/
That article should help out a bit...
Since you don't normally know what that posted data from HubSpot looks like just yet, you can use a service like requestbin to view the data that is being posted.. Note: they might have a cap on the amount of JSON it can handle in the request so you might need to find another service that gives you the complete JSON data or create a script on your server that saves the request to a file. For requestbin, you would create a workflow in hubspot with the action being a webhook and set the type to post and the url to the url that you received when creating a new requestbin.
Once you know what the JSON data structure is that is coming into your server, you can start to go to work on manipulating that data.
It's hard to be sure from the info you are providing, but you are probably coming at this the wrong way around.
You probably need to write a php script that will be the target of a webhook. Here is a post that should get you started:
Catching json data from hubspot webhook
I've got a cURL request that works from the command line, but I can't figure out how to translate it into the PHP implementation of cURL. I believe that my issue is with the formatting of the data that's being sent, but I'm not 100% sure that is the case. This is the curl command I want to send:
# curl -X PUT -d '{"shared_link": {"access":"open"}}' \
-H "Authorization: Bearer ACCESSCODE" https://api.example.com/files/12345
I know that command works! So here's how I'm trying to reproduce it in PHP (where I have a variety of other curl commands working, but none quite like this).
$url = 'https://api.example.com/files/1234';
$header = array('Authorization: Bearer ACCESSCODE');
$data = array('shared_link'=>array('access'=>'open'));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PUT');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
$output = curl_exec($ch);
curl_close($ch);
return $output;
What I expect to get (and what I do get, when running the command from the command line) is a JSON response from the API server containing all the info about the file, including a change to the 'shared_link' value. What I get instead is a JSON response containing all the info about the file, with the 'shared_link' value unchanged. This is identical to if I were sending a GET request, or a PUT request where the format of the data was valid, but which didn't match any of the file's fields that were possible to update.
So I don't know if the problem with my request is the format of the data (this is what I think is the most likely) or whether I'm correctly configuring curl to do a PUT. I believe I'm doing that the way I've seen it described in a number of other examples, but it still seems a bit strange to me, so I can't be totally sure I'm doing it correctly. In addition, there could be some other area where I'm making a mistake that I don't recognize!
I've tried a number of different ways of formatting the data, none of which worked for me, including:
// This fails with 400 bad request
$data = http_build_query(array('shared_link'=>array('access'=>'open')));
// So does this
$data = urlencode("shared_link[access]=open");
// This fails because the JSON gets converted into objects,
// which POSTFIELDS won't accept
$data = json_decode('{"shared_link": {"access":"open"}}');
I'm running out of things to try. Can someone help me figure out what I'm doing wrong? And if there's any relevant information that I've left out, just let me know and I'll be happy to provide it.
Thanks in advance.
EDIT:
So the answer, it turns out, was so obvious that I overlooked it!
All I had to do was:
$data = '{"shared_link": {"access": "open"}}'
So yeah, question answered. Thanks CBroe!
In my (sort of) defense, the documentation for CURLOPT_POSTFIELDS (http://php.net/manual/en/function.curl-setopt.php) says that the value for that option has to be either an array or a urlencoded string, which is what I was going on. So I was working under that assumption, which was clearly mistaken, since what worked is definitely neither an array nor a urlencoded string.
You can try this
$post_body = http_build_query(
array(
'shared_link[access]' => 'open'
)
);
After that, pass curl opt as curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode($post_body));
I have a PHP script that syncs data with a third party service, and I would like to, if possible, replace nuSOAP with cURL as I have heard cURL is faster. The web service I am calling just takes simple HTTP post and returns it, so the cURL parameters shouldn't be too involved.
I need to pass 4 things, a user id, password, organization id, and the name of the web service to receive data from.
Which part of the cURL options do I pass them? I was trying to pass them in the header, but I am not sure if that is correct. I kept receiving 'Bad Request (Invalid Number)' error.
Edit: I am setting the HTTPHEADER but it looks like its still setting it to text/html.
Since, i have a thought that you have some basic understanding of cURL. I am giving you some shallow information.
If you are just posting some information to a page make use of
curl_setopt($agent, CURLOPT_POST, true);
curl_setopt($agent, CURLOPT_POSTFIELDS, $post_data);
where $post_data will be the information you post to the page , something like
$post_data="name=stanley&feedback=good";
Or
If you are trying to make an authentication to a page, Just use
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt(CURLOPT_USERPWD, '[username]:[password]');
If you say 'The web service I am calling just takes simple HTTP post', assuming it doesn't use SOAP, you would do it by:
curl_setopt($handle,CURLOPT_POST,true);
curl_setopt($handle,CURLOPT_POSTFIELDS,array(
'user_id' => 'user',
'password' => 'pass' //etc, all the key/value pairs you need.
));
However, if it is a SOAP service, you would have to make a SOAP request, and which for it would take we cannot tell you without a WSDL. Any of the PHP XML packages would do to create it, possibly for simple things even normal string manipulation.
A help in the built-in soapclient (not nusoap) would be to do a request with SOAPClient and just examine the output of __getLastRequest().
I have been given API documentation which I don't quite get as there is no URL to connect up to?
http://support.planetdomain.com/index.php?_m=downloads&_a=viewdownload&downloaditemid=14&nav=0
I'd prefer doing this in PHP..
How can I run a 10 iteration loop, doing a check if domain is available, if it's response is available, then perform the register command and exit the script (using the code provided in thd documentation).
Thank you.
For the basics, I suggest using cURL to access resources by HTTP POST.
I put this into a function:
function api_call($url,$data,$timeout=20)
{
$response=false;
$ch=curl_init($url);
curl_setopt_array($ch,array(CURLOPT_RETURNTRANSFER=>true,CURLOPT_NOBODY=>false,CURLOPT_TIMEOUT=>$timeout,CURLOPT_FORBID_REUSE=>1,CURLOPT_FRESH_CONNECT=>1,CURLOPT_POST=>true));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//this is an array containing the data you're sending them - an associative array describing which call.
//data example:
//array('operation'=>'user.verify','admin.username'=>'you','admin.password'=>'pass','reseller.id'=>'xxx')
$response=curl_exec($ch);
$status_code=intval(curl_getinfo($ch,CURLINFO_HTTP_CODE));
curl_close($ch);
return array('status'=>$status_code,'url'=>$url,'data'=>$response);
}
However, you need to supply a URL. Lucanos noted in the comments it is "api.planetdomain.com/servlet/TLDServlet".
http://support.planetdomain.com/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=77
by the way, I only use cURL for GET requests, so I might be missing some details on how to do a POST right. I tried to fill it in, though.
You ask "How can I run a 10 iteration loop, doing a check if domain is available, if it's response is available, then perform the register command and exit the script (using the code provided in thd documentation)."
Well, here's some pseudocode mixed with valid PHP. I don't know the domainplanet API as you know, so this will NOT work as is but it should give you a decent idea about it.
for($i=0;$i<10;$i++)
{
//set up the domain check call
$domains=array('futunarifountain.co.uk','megahelicopterunicornassaultlovepageant.ly');
$domain_check_call=array('domain.name'=>$domains[$i]);
$domain_info=api_call($dp_base_url,$domain_check_call);
$info=json_decode($domain_info,true);//IF they use JSON and not XML or something
if($info['domain']['status']=='available')
{
$register_call=something();//make the API calls to register the domain, similar to the above
if($register_call['success']){ exit();/*or whatever*/ }
}
}
Hope that helps get you on the right track.