cURL command to NSURLRequest - php

I want to convert cURL command:
curl -F 'access=xxxx' \
-F 'message=Hello.' \
-F 'picture=#1.jpg' \
https://example.com
to a NSURLRequest.
Can somebody please help me with this?

If you want to create a request with POST data, I suggest you to use ASIHTTPRequest: http://allseeing-i.com/ASIHTTPRequest
It is quite powerful and easy to use, you should take a look at the ASIFormDataRequest class (see the How To Use section).
I've never used it for SSL connections though, but I'm pretty sure it's possible.

Related

Why is PHP Curl returning T_CONSTANT_ENCAPSED_STRING?

I'm attempting to use PHP to get data from CallRail.com API via JSON.
However, I'm getting this error when previewing my PHP file directly in my browser: T_CONSTANT_ENCAPSED_STRING
My code:
<?
curl -H "Authorization: Token token={my-token-id}" \
-X GET \
"https://api.callrail.com/v2/a/{my-account-id}/calls.json"
?>
So far I have:
Read several articles related to the error without finding a solution or verifiably accurate explanation.
I've manually retyped all of the code to ensure I didn't have any improperly encoded characters or invalid white spaces.
I've also attempted to locate official documentation in the PHP docs but not found anything helpful.
And I've tested this in a blank PHP file so the code is completely isolated.
I don't know how to further debug this issue and am hoping someone can either identify the problem or share the steps to debug this on my own from this point.
Your code sample is binary, to be executed from the command line in a shell like Bash, not PHP. This is why you're getting that error.
If you want PHP to call an external binary use exec(), shell_exec() or system().
Also, for portability, don't use the short open tags, ever, because it depends on the short_open_tag php.ini directive, or whether or not PHP was compiled with --enable-short-tags.
Even if your code doesn't have to be portable, it's just a bad habit. In case you ever need to work with some portable PHP code in the future; try:
<?php
echo shell_exec('curl -H "Authorization: Token token={my-token-id}" -X GET "https://api.callrail.com/v2/a/{my-account-id}/calls.json"');
Or if you want it on multiple lines, you could use implode:
<?php
echo shell_exec ( implode ( " ", [
'curl',
'-H "Authorization: Token token={my-token-id}"',
'-X GET',
'"https://api.callrail.com/v2/a/{my-account-id}/calls.json"'
] ) );

How to allow dots in Slim 3 or nikic/FastRoute parameters?

I am using Slim 3, which uses nikic/FastRoute, and am having trouble with an endpoint like so:
$app->get('/count/{url}', function ($request, $response) use ($curl) {
$controller = new Proximate\Controller\CountUrl($request, $response);
$controller->setCurl($curl);
return $controller->execute();
});
My plan is to pass an urlencoded URL into {url} and inject that into the controller. For example, for http://www.example.com, the request would be:
curl \
--verbose \
http://localhost:8080/count/http%3A%2F%2Fwww.example.com%2F
However, this fails with a 404, so is evidently not matched. This also fails:
curl \
--verbose \
http://localhost:8080/count/http%3A%2F%2Fwww.
However, oddly, this does match (i.e. without the trailing dot):
curl \
--verbose \
http://localhost:8080/count/http%3A%2F%2Fwww
I originally thought it was the urlencoded slashes that was confusing it (%2F) but having tried it without these characters, I find that in fact it is the dot, anywhere in the string. Why does this not match, and do I need a regex match in order to get this to work?
I am using the PHP built-in web server for this app.
After a bit more digging, I find that this is caused by the PHP built-in web server, and is not a Slim or FastRoute issue at all. Here is the Slim bug report and here is one for PHP.
Sadly the core PHP team have marked as Won't fix, since the server is only for testing. Personally, I think the warnings about not using this server for production are a bit overblown (my Docker containers remain of a reasonably manageable size because I am not throwing Apache in there as well).
Thankfully there is a fix for this - specifying the script name in the URL will cause PHP to pass the rest of it correctly to the routing system. Like so:
curl \
--verbose \
http://localhost:8080/index.php/count/http%3A%2F%2Fwww.example.com%2F
# ^^^^^^^^^
# Script name
Of course, this is not very elegant, so I may yet switch to another solution. I've not tried it yet, but this PHP-only web server looks quite promising.

A better way of putting allot of information into an SQL database, than querying in URL

I have a simple php script set up, through which I put information into an MySQL database by the following URL query:
https://www.thesite.com/addinformation.php?WHERE_TO_LABEL_IT_AS&WHAT_TEXT_TO_ADD"
I call it from bash by simply using wget
wget --no-check-certificate --user=username --password='password' --delete-after https://www.thesite.com/addinformation.php?$LABELVARIABLE&$STRINGVARIABLE"
It works for my needs, but I would now like to use a similar way to add large chunks of text into the database. Perhaps even whole text files. What method would you recommend I read up on to do this?
You could save your content to a file, and post it using either wget or curl.
$ wget --post-file=<file path>
or
$ curl --data-binary #<file path>

following url: curl couldn't resolve host 'GET'

I have a page (realized with a php framework) that add records in a MySQL db in this way:
www.mysite.ext/controller/addRecord.php?id=number
that add a row in a table with the number id passed via post and other informations such as timestamp, etc.
So, I movedo my eintire web applicazione to another domain and all HTTP requests works fine from old to new domain.
Only remaining issue is the curl: I wrote a bash script (under linux) that run curl of this link. Now, obviously it does not works because curl returns an alert message in which I read the page was moved.
Ok, I edited the curl sintax in this way
#! /bin/sh
link="www.myoldsite.ext/controlloer/addRecord.php?id=number"
curl --request -L GET $link
I add -L to follow url in new location but curl returns the error I wrote in this topic title.
It would be easier if I could directly modify the link adding the new domain but I do not have physical access to all devices.
GET is the default request type for curl. And that's not the way to set it.
curl -X GET ...
That is the way to set GET as the method keyword that curl uses.
It should be noted that curl selects which methods to use on its own depending on what action to ask for. -d will do POST, -I will do HEAD and so on. If you use the --request / -X option you can change the method keyword curl selects, but you will not modify curl's behavior. This means that if you for example use -d "data" to do a POST, you can modify the method to a PROPFIND with -X and curl will still think it sends a POST. You can change the normal GET to a POST method by simply adding -X POST in a command line like:
curl -X POST http://example.org/
... but curl will still think and act as if it sent a GET so it won't send any request body etc.
More here: http://curl.haxx.se/docs/httpscripting.html#More_on_changed_methods
Again, that's not necessary. Are you sure the link is correct?

cURL able to make cookie but not use it

I'm trying to script bash to do a few simple curl commands.
I can generate a cookie that I want to use with the following command:
curl -c cookie --data "user=user&password=pass" //example/login.php
However, when I try passing it into the site again the cookie is ignored. It's as if I didn't even login into the first place. The command I'm using is the following:
curl -b cookie //example/
What am I doing wrong?
Any help would be appreciated thanks.
it turns out I wasn't generating my cookie correctly after all.
I was actually missing some additional variables that were required in my POST statement, which interacted with the php log in script I was using.
You may want to store the cookie that comes back. You do so by specifying a cookie file:
curl -c cookies.txt -d "user=user&password=pass" //example/login.php
and to use those cookie in later requests you do:
curl -b cookies.txt //example/

Categories